Javascript 表单 onsubmit 与提交按钮 onclick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6908187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form onsubmit versus submit button onclick
提问by mplungjan
UPDATEFrom @BrendanEich?
来自@BrendanEich 的更新?
@mplungjan onclick of submit just falls out of that being a button; form onsubmit is clearly better.
@mplungjan 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?
使用提交按钮的 onclick 来确定是否应该提交表单的原因是什么?
I believe strongly that
我坚信
- to execute something before submit and cancel the submit in case of error, the form's onsubmit event is the obvious place to put it
- If you use the onclick of a submit button and later decide to use type="image" the event handler will fail in many browsers
- if you change the submit to a button, you will have to add a submit to the onclick event handler too.
- 在提交之前执行某些操作并在出现错误时取消提交,表单的 onsubmit 事件是放置它的明显位置
- 如果您使用提交按钮的 onclick 然后决定使用 type="image" 事件处理程序将在许多浏览器中失败
- 如果将提交更改为按钮,则还必须向 onclick 事件处理程序添加提交。
I am looking for strong reasons to prefer using a submit button's onclick event over the form's onsubmit.
我正在寻找更喜欢使用提交按钮的 onclick 事件而不是表单的 onsubmit 的充分理由。
UPDATE: Please not that I am personally well aware of many of the issues around form submission and validation.
For example that submitting by javascript will not trigger the onsubmit
http://jsfiddle.net/mplungjan/3A4Ha/
更新:请注意,我个人非常了解有关表单提交和验证的许多问题。
例如通过javascript提交不会触发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>
Also that IE will not trigger the onclick if you hit enter in the form in my fiddle
此外,如果您在我的小提琴中的表单中按 Enter 键,IE 将不会触发 onclick
History:
历史:
Got into a discussion here
在这里讨论
html button not clickable without being disabled
I have an intense dislike of using the onclick of a submit button for ANYTHING due to many1years 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.
我非常不喜欢使用提交按钮的 onclick 进行任何事情,因为很多1年来看到这在许多浏览器中不起作用,主要是旧版本的 IE。我已经列出了一些显而易见的原因,但我相信它们不会说服顽固的用户。
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.
SO 的社区能帮我把这个钉在墙上吗,就像他们钉 w3schools 一样?也随时就我如何以可接受的方式表达这个问题发表评论。
1: since the days of NS2.x and IE3.02
1:从 NS2.x 和 IE3.02 时代开始
回答by Madara's Ghost
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.
Form onsubmit 是一种更正确的方法,原因很简单,表单也可以使用<ENTER>
key提交,而不仅仅是通过单击提交按钮。
回答by Mx.Wolf
There are no direct reasons to use a from's onsubmit handler over a button's onclick handler. One should use named events according to their intent.
没有直接理由在按钮的 onclick 处理程序上使用 from 的 onsubmit 处理程序。人们应该根据他们的意图使用命名事件。
You may consider a scenario with two actions – a “send” action (as in “to send an e-mail”) and a “save” (as in “draft”). The former action assumes the form filled in a coherent manner, while the latter action should allow the user to persist whatever state the user likes. That makes the click handlers ideal places to decide on validation and cancellation. The relevance of the submit event in this case is undetermined.
你可以考虑一个有两个动作的场景——“发送”动作(如“发送电子邮件”)和“保存”(如“草稿”)。前一个动作采用以连贯方式填充的表单,而后一个动作应该允许用户保持用户喜欢的任何状态。这使得点击处理程序成为决定验证和取消的理想场所。在这种情况下提交事件的相关性是不确定的。
To make the matter even more complicated you may add auto-save and optimistic concurrency to the scenario.
为了让事情变得更加复杂,您可以在场景中添加自动保存和乐观并发。
IMHO The years of tries and errors in the matter resulted in the HTML5 standard to have ruled the question in favor of the button element. See button's [form*] attributes that allow to change the form action, method by each button individually.
恕我直言,多年来在这件事上的尝试和错误导致 HTML5 标准对这个问题做出了有利于按钮元素的裁决。查看按钮的 [form*] 属性,这些属性允许单独更改每个按钮的表单操作和方法。