阻止按钮在输入密钥上提交 [英] Prevent a button to submit on enter key

查看:76
本文介绍了阻止按钮在输入密钥上提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮,一个是id = enterToSubmit,另一个是id = clickToSubmit。当用户点击enterkey时,我想要enterToSubmit按钮提交表单
而不是clickToSubmit按钮。

I have two buttons one with id=enterToSubmit and other with id=clickToSubmit. When a user hit the enterkey, I want "enterToSubmit" button to submit form not the "clickToSubmit" button.

在同一页面上,我有一个textbox id = title,并在页面加载时隐藏enterToSubmit按钮,直到用户在文本框中输入内容,然后用户可以按Enter键提交表单。

On the same page, I have a textbox id=title, and on Page Load I hide "enterToSubmit " button, until user enters something in the textbox, then users can hit Enter key to submit form.

如何用户按Enter键时会阻止clickToSubmit按钮提交吗?

How do preven "clickToSubmit" button to submit when users press Enter key?

推荐答案

不幸的是你不能改变'默认'提交HTML中的按钮,它总是第一个输入 / 按钮,类型为提交 / 图片

Unfortunately you can't change the ‘default’ submit button in HTML, it's always the first input/button with type submit/image.

虽然您可以尝试捕获Enter keypress 手动事件这样做是非常不可靠的,因为Enter并不总是应该提交表单,这取决于(a)关注的元素类型,(b)是否使用shift / ctrl + enter ,(c)形式中的其他元素和(d)它是什么浏览器。如果你错过了一个案例,你会得到意外的默认提交;如果你遇到一个你不应该的情况,你会得到意外的输入提交。

Whilst you can attempt to catch Enter keypress events manually it is highly unreliable to do so, since Enter isn't always supposed to submit a form, depending on (a) what type of element is focused, (b) whether shift/ctrl+enter is used, (c) what other elements are in the form and (d) what browser it is. If you miss a case you will get accidental default-submissions; if you hit a case you shouldn't, you'll get accidental enter-submissions.

所以当你想控制默认提交动作时通常会更好一个表单,将一个额外的按钮作为表单中的第一个提交按钮。如果您不希望显示此按钮,则可以使用较大的负值将其置于页面一侧。 (这有点难看,但通过 display visibility 隐藏它会阻止它在某些浏览器中运行。)

So it is in general better, when you want to control the default submission action of a form, to put an extra button in as the first submit button in the form. If you don't want this button to be displayed, you can position it with a large negative left value to push it off the side of the page. (This is a bit ugly, but hiding it via display or visibility will stop it working in some browsers.)

所以也许你可以隐藏输入提交按钮,而不是让它消失,而是将其推离无法看到的页面,然后抓住点击事件到返回false 并停止表单提交。

So perhaps you could ‘hide’ the enter-to-submit button not by making it disappear but by pushing it off the page where it can't be seen, and then catching the click event on it to return false and stop the form submission.

这篇关于阻止按钮在输入密钥上提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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