问题描述
- 为什么这段代码onClick无效啊
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'cart1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript"> function jian() { alert(" ---"); } function addd() { alert(" ++="); } </script> </head> <body> <form method="post" > <table border=1 align="center"> <tr> <td colspan="4" align="center">订单</td> </tr> <tr> <td>商品</td> <td>数量</td> <td>总价</td> </tr> <tr> <td>aaa</td> <td><input type="button" name="jian" onClick="jian()" value="-"></button> <input name="shuru" class="input_text" type="text" value="1" size="3"> <button name="addd" onClick="addd()">+</button></td> <td><input name="cost" class="input_text" type="text" value="0" size="3" value="0"></td> </tr> <tr> <td colspan="4" align="center"> <button id="buy_anniu" type="submit" onClick="but()">提交</button> <button type="reset">重置</button> </td> </tr> </table> </form> </body> </html>
解决方案
不要用button标签,用input type='button',你用button标签再标准浏览器下是提交按钮,直接提交表单了,而且你的输入控件对象不要将name和你的函数名起一样,会导致在onclick引用到这个控件,而不是你的函数
<tr>
<td>aaa</td>
<td><input type="button" onClick="jian()" value="-">
<input name="shuru" class="input_text" type="text" value="1" size="3">
<input type="button" onClick="addd()" value="+"/></td>
<td><input name="cost" class="input_text" type="text" value="0" size="3"
value="0"></td>
</tr>
时间: 2024-09-14 12:09:00