FORM表单中onclick()、submit()与onsubmit()的问题
2016-01-05 12:04
447 查看
最近遇到一次处理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();” 才能取得函数的返回值,否则只是调用函数,返回值未被传递
正确写法:
于是仔细研究了下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();” 才能取得函数的返回值,否则只是调用函数,返回值未被传递
正确写法:
相关文章推荐
- 在线用表单建立文件夹
- Jquery 表单取值赋值的一些基本操作
- javascript 表单的友好用户体现
- js 提交和设置表单的值
- 攻克CakePHP系列二 表单数据显示
- JavaScript获取页面中表单(form)数量的方法
- 网页中表单按回车就自动提交的问题的解决方案
- 分享纯手写漂亮的表单验证
- js实现不提交表单获取单选按钮值的方法
- js实现文件上传表单域美化特效
- JavaScript实现重置表单(reset)的方法
- javascript实现自动填写表单实例简析
- JavaScript实现同一页面内两个表单互相传值的方法
- JS中表单的使用小结
- 封装好的一个万能检测表单的方法
- Bootstrap每天必学之表单
- javascript创建动态表单的方法
- JS+DIV+CSS实现仿表单下拉列表效果
- jQuery+CSS3折叠卡片式下拉列表框实现效果
- jquery实现简洁文件上传表单样式