您当前的位置: 首页 > 前端教程 > HTML教程 > FORM表单中onclick()、submit()与onsubmit()的问题

FORM表单中onclick()、submit()与onsubmit()的问题

作者:xiaoxiao 来源:未知 发布时间: 2013-10-24 09:27 点击:
最近遇到一次处理form数据的过滤,采用了button的onclick事件来检查,发现return false后表单仍然提交了。 于是仔细研究了下onclick、onsubmit、submit集合函数之间的关系和区别 onsubmit: You can override this event by returning false in the event handler. Use

FORM表单中onclick()、submit()与onsubmit()的问题

      最近遇到一次处理form数据的过滤,采用了button的onclick事件来检查,发现return false后表单仍然提交了。
  于是仔细研究了下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会继续

分享到:
本文"FORM表单中onclick()、submit()与onsubmit()的问题"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(3)
100%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4