jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here </title><script type="text/javascript">
function goSubmit()
{
var frm=document.forms["EquipForm"];
frm.submit();
}
</script>
</head>
<body>
<form action="addType.do" onsubmit="return false;">
<table>
<tr>
<td>equip: <input type="text" name="equip"> </td>
</tr>
<tr>
<td> <input type="button" onclick="goSubmit()" value="OK"> </td>
</tr>
</table>
</form>
</body>
</html>以下是struts-config.xml文件<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans><form-bean type="org.apache.struts.validator.DynaValidatorForm" name="EquipForm">
<form-property type="java.lang.String" name="equip" />
</form-bean></form-beans><action-mappings>
<action path="/addType" type="action.AddAction">
<forward path="index.jsp" name="finish"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts.action.RequestProcessor" contentType="text/html;charset=UTF-8" nocache="true" />
<message-resources parameter="application"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
</struts-config>
这样为何提交不了?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here </title><script type="text/javascript">
function goSubmit()
{
var frm=document.forms["EquipForm"];
frm.submit();
}
</script>
</head>
<body>
<form action="addType.do" onsubmit="return false;">
<table>
<tr>
<td>equip: <input type="text" name="equip"> </td>
</tr>
<tr>
<td> <input type="button" onclick="goSubmit()" value="OK"> </td>
</tr>
</table>
</form>
</body>
</html>以下是struts-config.xml文件<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans><form-bean type="org.apache.struts.validator.DynaValidatorForm" name="EquipForm">
<form-property type="java.lang.String" name="equip" />
</form-bean></form-beans><action-mappings>
<action path="/addType" type="action.AddAction">
<forward path="index.jsp" name="finish"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts.action.RequestProcessor" contentType="text/html;charset=UTF-8" nocache="true" />
<message-resources parameter="application"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
</struts-config>
这样为何提交不了?
function goSubmit()
{
var frm=document.forms["EquipForm"];
frm.submit();
}
</script>
</head> 这部分不对
应该
表单名.submit();
但是你的没有表单名
而是<form action="addType.do" onsubmit="return false;"> 这个也有错
<form action="addType.do" name="frm">
{
document.forms[0].submit();
}
</script>
frm.action="addType.do"
frm.submit();
呵呵 tantaiyizu 真搞
用代码提交不触发onsubmit事件.....楼主表单少了name属性,要不就document.forms[0].submit()//提交第一个表单,如果有多个的话注意form出现的顺序
如果直接 document.forms[0].submit() 不触发submit事件,大家可以考虑下合理吗?从大的方面考虑,不管你以什么样的形式去提交,都应该去触发 onsubmit事件 ,你想想 on是什么意思 ,就是当提交的时候要发生的。
如果确实是这样 ,那么我只能说浏览器在这点上设计的不合理 ,当然就是不合理我们也没办法 ,不知道大家怎么认为的?
晕 你认为这个搞是讽刺的话 那我道歉 我这个搞是针对你的frm.submit() 还有鸡巴用?
你的提议当然我会反省
是他触发的事件 ,你既然触发了 ,但要是程序不走 onsubmit ,你个人觉得合理吗?希望你能再考虑考虑
tantaiyizu 痴情客 仔细看看1楼和6楼,看清楚了再说,ok?
我已经仔细看过了, 很明显 从头到尾我的关注点不在什么 表单名字上面 我的关心的是大家对 onsubmit 的理解
但是你的没有表单名
而是 <form action="addType.do" onsubmit="return false;"> 这个也有错
<form action="addType.do" name="frm">
人家当然说了
你自己没看见他这个问题主要是没有 name
不然你就像他们说得那样指定form的name
document.forms['name']
你干吗要去猜测呢?
为什么一定要认为你的想法是对的??
你不去实践证明,在这乱猜测有什么用
<form action="addType.do" onsubmit="return false;">
<table>
<tr>
<td>equip: <input type="text" name="equip"> </td>
</tr>
<tr>
<td> <input type="button" onclick="goSubmit()" value="OK"> </td>
</tr>
</table>
</form> 你自己好好看你这部分
你的form是什么!!
还是onsubmit="return false;"你这个返回false如果提交??
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here </title> <script type="text/javascript">
function goSubmit()
{
var frm=document.forms["EquipForm"];
frm.submit();
}
</script>
</head>
<body>
<form action="addType.do" onsubmit="return false;">
<table>
<tr>
<td>equip: <input type="text" name="equip"> </td>
</tr>
<tr>
<td> <input type="button" onclick="goSubmit()" value="OK"> </td>
</tr>
</table>
</form>
</body>
</html> 修改为:
jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here </title> <script type="text/javascript">
function goSubmit()
{
var frm=document.forms[0];
frm.submit();
}
</script>
</head>
<body>
<form action="addType.do" onsubmit="return false;">
<table>
<tr>
<td>equip: <input type="text" name="equip"> </td>
</tr>
<tr>
<td> <input type="button" onclick="goSubmit()" value="OK"> </td>
</tr>
</table>
</form>
</body>
</html>
后面的意思应该是既然提交,就应该触发onsubmit="return false",而不应该忽略.
2. <input type="button" onclick="goSubmit()" value="OK"> 改为 <input type="submit" value="OK">或者你不嫌麻烦非得使用脚本提交并坚定地要用户承受禁用脚本的痛苦:
<%@ page language="java" pageEncoding="ISO-8859-1" contentType="text/html; charset=ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here </title>
<script type="text/javascript">
function doSubmit() {
var frm = document.getElementById("yourform"); //兼容性比较好IE火狐都适用
//校验输入域代码可以放在这儿,校验不通过下面一句不执行。
//如果用户禁用脚本你的表单打死也别想正常提交。想彻底解决问题接着往下看...
frm.submit();
}
</script>
</head>
<body>
<form id="yourform" action="addType.do">
<table>
<tr><td>equip: <input type="text" name="equip"></td></tr>
<tr><td><input type="button" value="OK" onClick="doSubmit();"></td></tr>
</table>
</form>
</body>
</html>
否则按下里巴人的(通俗的)做法:
<%@ page language="java" pageEncoding="ISO-8859-1" contentType="text/html; charset=ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here </title>
<script type="text/javascript">
function validate() {
//校验输入域代码放这儿
if (/*输入域校验通过*/) {
return true;
}
return false;
}
</script>
</head>
<body>
<form action="addType.do" onSubmit="return validate();">
<table>
<tr><td>equip: <input type="text" name="equip"></td></tr>
<tr><td><input type="submit" value="OK"></td></tr>
</table>
</form>
</body>
</html>
你加这段代码是干吗吃地/
只有在表单按钮提交的时候才会走这个
例如
<input type="submit" value="提交"/> 点击这个的时候才会走return false.但是如果你在这里写了onclick=
例如 :
<input type="submit" value="提交" onclick="return test()"/>
那么就不会走return false; 而是去走test的 如果test里返回空 那么还是走return false;
如果return true; 那么就直接走true了。:)
这样
如果不加的话,别人在input里按回车就可以提交表单了,根本不用点ok按钮。