JS中 submit提交与Form表单里的onsubmit的调用问题?

最近在开发中遇到了表单提交前验证的问题,用一个普通的button按钮代替submit按钮,
在提交前触发这个button的onclick事件,在其事件中触发form的submit事件。问题出现了:

<form action="http://www.baidu.com/s?wd=this.form.submit%28%29%3B&cl=3" method="post" name="form1" onsubmit="return alert('已提交!'); return false;">
<table align="center" width="420px" cellPadding="2" cellSpacing="1" bgcolor="#A4B6D7" style="word-wrap:Break-word;">
<tr style="cursor: hand;background:#d7e3f6" >
<td width="20%" align="right">条型码</td>
<td><input style="width:90%" type="text" name="GOODSNUM" size="30" maxlength="8" ></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="button" name="save" value="保存" onclick="if((confirm('确定要提交吗?'))) this.form.submit();"/>
</td>
</tr>
</table>
</form>

  发现并没有触发form的onsubmit方法,而是直接提交了。(自己测得时候并没有这个情况而是通过onsubmit验证了)

就改成this.form.onsubmit();  成功!

submit这个方法是不触发onsubmit事件的,如果想要触发它,需要调用
fireEvent方法。尝试一下:this.form.fireEvent('onsubmit');哈哈,果然也成功!不过这样不是多此一举吗?呵呵!

this.form.submit();     //直接提交表单
this.form.onsubmit();   //调用form的onsubmit方法
this.form.fireEvent('onsubmit'); //同上,
     PS:又学到了fireEvent这个方法,

上一篇:HDU 4082 Hou Yi's secret --枚举


下一篇:【Python】利用skorch进行深度学习