FORM表单中onclick()、submit()与onsubmit()的问题
2015-12-26 21:24
615 查看
最近遇到一次处理form数据的过滤,采用了button的onclick事件来检查,发现return false后表单仍然提交了。
于是仔细研究了下onclick、onsubmit、submit集合函数之间的关系和区别
首先生成一个form
自己写X()、Y()函数,我们会发现,这几个函数的执行顺序
1) onclick: Y();
2) onsubmit: X();
3) submit();
也就是说
只要 onclick 未 return false 那么就继续执行 onsubmit
只要 onsubmit 未return false 那么表单就被提交出去了
另外一点写法上注意一定要 “return X();” 才能取得函数的返回值,否则只是调用函数,返回值未被传递
正确写法:
<input type=submit onclick=”return X();”>
//X() 返回false后,form的submit会被终止
错误写法:
<input type=submit onclick=”X()”>
//X() 返回false后未传递给onclick事件,form的submit会继续
于是仔细研究了下onclick、onsubmit、submit集合函数之间的关系和区别
onsubmit: You can override this event by returning false in the event handler. Use this capability to validate data on the client side to prevent invalid data from being submitted to the server. If the event handler is called by the onsubmit attribute of the form object, the code must explicitly request the return value using the return function, and the event handler must provide an explicit return value for each possible code path in the event handler function. The submit method does not invoke the onsubmit event handler. submit: 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
<form action="#" method="POST" name="A" onsubmit="return X();"> <input type="text" value="" /> <input onclick="Y()" type="submit" value="提交" /> </form>
自己写X()、Y()函数,我们会发现,这几个函数的执行顺序
1) onclick: Y();
2) onsubmit: X();
3) submit();
也就是说
只要 onclick 未 return false 那么就继续执行 onsubmit
只要 onsubmit 未return false 那么表单就被提交出去了
另外一点写法上注意一定要 “return X();” 才能取得函数的返回值,否则只是调用函数,返回值未被传递
正确写法:
<input type=submit onclick=”return X();”>
//X() 返回false后,form的submit会被终止
错误写法:
<input type=submit onclick=”X()”>
//X() 返回false后未传递给onclick事件,form的submit会继续
相关文章推荐
- Extjs4.0 最新最全视频教程
- Javascript中toFixed方法的改进
- 5个常见可用性错误和解决方案
- 在线用表单建立文件夹
- Jquery 表单取值赋值的一些基本操作
- js可突破windows弹退效果代码
- JSP脚本漏洞面面观
- 使用BAT一句话命令实现快速合并JS、CSS
- js显示当前星期的起止日期的脚本
- 爆炸式的JS圆形浮动菜单特效代码
- javascript 表单的友好用户体现
- js select常用操作控制代码
- JS实现不使用图片仿Windows右键菜单效果代码
- 从jsp发送动态图像
- 原生js结合html5制作小飞龙的简易跳球
- js 页面模块自由拖动实例
- js实现小鱼吐泡泡在页面游动特效
- js 提交和设置表单的值
- PHP VBS JS 函数 对照表
- 微信公众平台小程序开发大牛必备技能