QQ 1640076782

2013年03月1日

form表单提交 submit和onsubmit的区别

Filed under: phper — 标签: — lijie @ 9:05 上午

今天做程序遇到了一个很令人郁闷的问题,就是表单提交的时候有一个验证,如果必填选项未填写,那么该表单就不能提交,可

是我的却是照提交无误,我的代码如下

<form name=”form” method=”post” action=”contact_us.php” onsubmit=”return checkform();”>

<table>

<tr><td>name:<input type=”text” name=”name” value=”">*</td></tr>

<tr><td>tel:<input type=”text” name=”tel” value=”"></td></tr>

…..

<tr><td><input  type=”submit” value=”ok” name=”submit”></td></tr>

</table>

</form>

按照预期的效果走应该是 当名字未填写时checkform应该返回false 表单不提及,可是我的表单却是提交了,根本没有触发form

的onsubmit

然后逐行往下查找代码 发现提交方法用的是submit,突然间想到以前看过一篇文章说submit和onsubmit是有却别的,submit其

实是不触发onsubmit的,如果想要触发就必须要调用,然后又在网上查了资料,手册上是这样解释的:

The submit method does not invoke the onsubmit event handler. Call the onsubmit event handler directly. When using

Microsoft® Internet Explorer 5.5 and later, you can call the fireEvent method with a value of onsubmit in the sEvent

parameter

然后我将代码修改如下:

<form name=”form” method=”post” action=”contact_us.php” onsubmit=”return checkform();”>

<table>

<tr><td>name:<input type=”text” name=”name” value=”">*</td></tr>

<tr><td>tel:<input type=”text” name=”tel” value=”"></td></tr>

…..

<tr><td><input  type=”button” onclick=”this.form.onsubmit();” value=”ok” name=”submit”></td></tr>

</table>

</form>

这么一试 果然是正确的,原来submit和onsubmit的区别是在这里

实例网站:www.angelnew.com

如果你有外贸购物网站建站需求,欢迎随时联系我们

QQ客服:415682727

电话:020-39907297

MSN:[email protected]

全国免费电话:800-999-8262

更多 外贸网站建设案例》》

官网:https://www.onepound.cn