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

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

问题描述

更新 来自 @BrendanEich

@mplungjan提交的onclick只是一个按钮而已;表单onsubmit显然更好.

@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.

更新:请不要让我个人非常了解表单提交和验证方面的许多问题.
例如,使用javascript提交不会触发onsubmit http://jsfiddle.net/mplungjan/3A4Ha/

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/

<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时代起

推荐答案

表单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天全站免登陆