简体   繁体   English

如何检查是否单击了提交按钮而不是页面上的输入?

[英]How can I check if a submit button is clicked and not an enter press on a page?

Imagine this piece of html:想象一下这段 html:

​<form>
    <input type="text">
    <input type="submit" id="a">
</form>​​​​​​

and this piece of Javascript和这段 Javascript

document.getElementById('a').onclick = function (e) {
    alert(e.type);
};​​​​

The alert will always say 'click', even if you press enter in the textfield.即使您在文本字段中按回车,警报也会始终显示“点击”。

How can I check if the button is really clicked?如何检查按钮是否真的被点击?

Imagine this piece of html:想象一下这段 html:

<form>
    <input type="text">
    <input type="button" id="a">
</form>

and this piece of Javascript和这段 Javascript

document.getElementById('a').onclick = function (e) {
    this.form.submit();
};​​​​

No confusion will be possible.不会有任何混淆。

There is almost no reason to use input type="submit" in modern web applications.在现代 Web 应用程序中几乎没有理由使用input type="submit"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM