表单 onsubmit 与提交按钮 onclick [英] Form onsubmit versus submit button onclick

查看:29
本文介绍了表单 onsubmit 与提交按钮 onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新来自 @BrendanEich

@mplungjan onclick 提交只是不属于按钮;提交表单显然更好.

@mplungjan onclick of submit just falls out of that being a button; form onsubmit is clearly better.


使用提交按钮的 onclick 来确定是否应该提交表单的原因是什么?


Which would be the reasons to ever use the onclick of a submit button to determine whether or not the form should be submitted?

我坚信

  1. 在提交之前执行某些操作并在出错时取消提交,表单的 onsubmit 事件是放置它的明显位置
  2. 如果您使用提交按钮的 onclick 后决定使用 type=image";事件处理程序将在许多浏览器中失败
  3. 如果您将提交更改为按钮,您还必须将提交添加到 onclick 事件处理程序.

我正在寻找强有力的理由来更喜欢使用提交按钮的 onclick 事件而不是表单的 onsubmit.

I am looking for strong reasons to prefer using a submit button's onclick event over the form's onsubmit.

更新:请注意,我个人非常了解有关表单提交和验证的许多问题.

UPDATE: Please note that I am personally well aware of many of the issues around form submission and validation.

例如通过javascript提交不会触发onsubmithttp://jsfiddle.net/mplungjan/3A4Ha/

For example that submitting by javascript will not trigger the onsubmit http://jsfiddle.net/mplungjan/3A4Ha/

<form onsubmit="alert('onSubmit called')">
    <input type="text" value="This will submit on enter but in IE the onclick is not triggered" style="width:100%"/><br/>
    <input type="submit" onclick="alert('Clicked')" />
</form><br />
<a href="#" onclick="alert('Submitting by script'); return false">Submit by script will not trigger onsubmit</a>

如果你在我的小提琴中按回车键,IE 也不会触发 onclick

Also that IE will not trigger the onclick if you hit enter in the form in my fiddle

历史:

在这里讨论

html 按钮在没有被禁用的情况下不可点击

由于 许多1 年在许多浏览器(大多数是较旧的浏览器)中看到这不起作用,我非常不喜欢使用提交按钮的 onclick 进行任何操作IE版本.我列出了一些明显的原因,但我相信他们不会说服顽固的用户.

I have an intense dislike of using the onclick of a submit button for ANYTHING due to many1 years of seeing this not work in a number of browsers, mostly older version of IE. I have listed a few of the obvious reasons, but I am sure they will not convince the hardened user.

SO 的社区可以帮我把它钉在墙上,就像他们钉 w3schools 一样吗?也请随时就我如何以可接受的方式表达这个问题发表评论.

Can SO's community help me nail this one to the wall, like they nailed w3schools? Also feel free to give comments as to how I can phrase this question in an acceptable manner.

1:从 NS2.x 和 IE3.02 开始

推荐答案

Form onsubmit 是一种更正确的方法,原因很简单,表单也可以使用 <ENTER> 键提交,而不仅仅是点击提交按钮.

Form onsubmit is a more correct approach, for the simple reason that a form can also be submitted with the <ENTER> key, and not just by clicking the submit button.

这篇关于表单 onsubmit 与提交按钮 onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆