使用 JavaScript 在文本框中按 Enter 键触发按钮单击
我有一个文本输入和一个按钮(见下文)。 当在文本框中按下 Enter 键时,如何使用 JavaScript 触发按钮的单击事件?
我的当前页面上已经有一个不同的提交按钮,因此我不能简单地将该按钮设为提交按钮。 而且,我仅希望 Enter 键在从该文本框内按下时单击此特定按钮,仅此而已。
<input type="text" id="txtSearch" />
<input type="button" id="btnSearch" value="Search" onclick="doSomething();" />
I have one text input and one button (see below). How can I use JavaScript to trigger the button's click event when the Enter key is pressed inside the text box?
There is already a different submit button on my current page, so I can't simply make the button a submit button. And, I only want the Enter key to click this specific button if it is pressed from within this one text box, nothing else.
<input type="text" id="txtSearch" />
<input type="button" id="btnSearch" value="Search" onclick="doSomething();" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
在 jQuery 中,以下代码可以工作:
或者在纯 JavaScript 中,以下代码可以工作:
In jQuery, the following would work:
Or in plain JavaScript, the following would work:
然后只需将其编码即可!
Then just code it in!
弄清楚了这一点:
Figured this out:
让按钮成为提交元素,这样它就会自动进行。
请注意,您需要一个包含输入字段的
重新定义标准行为不是一个好习惯,Enter 键应始终调用表单上的提交按钮。
Make the button a submit element, so it'll be automatic.
Note that you'll need a
<form>
element containing the input fields to make this work (thanks Sergey Ilinsky).It's not a good practice to redefine standard behaviour, the Enter key should always call the submit button on a form.
由于还没有人使用过
addEventListener
,这里是我的版本。 给定元素:我将使用以下内容:
这允许您单独更改事件类型和操作,同时保持 HTML 干净。
Since no one has used
addEventListener
yet, here is my version. Given the elements:I would use the following:
This allows you to change the event type and action separately while keeping the HTML clean.
在纯 JavaScript 中,
我注意到回复仅在 jQuery 中给出,因此我考虑也用纯 JavaScript 给出一些内容。
In plain JavaScript,
I noticed that the reply is given in jQuery only, so I thought of giving something in plain JavaScript as well.
在现代 JS 中使用
keypress
和event.key === "Enter"
!Mozilla 文档
支持的浏览器
Use
keypress
andevent.key === "Enter"
with modern JS!Mozilla Docs
Supported Browsers
您可以使用一个基本技巧来实现这一点,但我还没有看到完全提及。 如果您想在 Enter 上执行 ajax 操作或其他一些工作,但不想实际提交表单,您可以执行以下操作:
设置 action="javascript:void(0);" 这是从本质上防止默认行为的捷径。 在这种情况下,无论您按下 Enter 键还是单击按钮,都会调用一个方法,并且会调用 ajax 来加载一些数据。
One basic trick you can use for this that I haven't seen fully mentioned. If you want to do an ajax action, or some other work on Enter but don't want to actually submit a form you can do this:
Setting action="javascript:void(0);" like this is a shortcut for preventing default behavior essentially. In this case a method is called whether you hit enter or click the button and an ajax call is made to load some data.
要在每次按下 Enter 键时触发搜索,请使用以下命令:
To trigger a search every time the enter key is pressed, use this:
尝试一下:
Try it:
纯 JS 工作时间短
Short working pure JS
这只是我从最近的一个项目中得到的东西......我在网上找到了它,我不知道在普通的旧 JavaScript 中是否有更好的方法。
This is just something I have from a somewhat recent project... I found it on the net, and I have no idea if there's a better way or not in plain old JavaScript.
在现代的、未弃用的(没有
keyCode
或onkeydown
)Javascript 中:In modern, undeprecated (without
keyCode
oronkeydown
) Javascript:尽管如此,我很确定只要表单中只有一个字段和一个提交按钮,按 Enter 键就应该提交表单,即使页面上还有另一个表单。
然后,您可以使用 js 捕获表单 onsubmit 并执行您想要的任何验证或回调。
Although, I'm pretty sure that as long as there is only one field in the form and one submit button, hitting enter should submit the form, even if there is another form on the page.
You can then capture the form onsubmit with js and do whatever validation or callbacks you want.
这是适合所有 YUI 爱好者的解决方案:
在这种特殊情况下,我确实使用 YUI 获得了更好的结果触发已注入按钮功能的 DOM 对象 - 但这是另一个故事了......
This is a solution for all the YUI lovers out there:
In this special case I did have better results using YUI for triggering DOM objects that have been injected with button functionality - but this is another story...
在 Angular2 中:
如果您不希望在按钮中出现一些视觉反馈,那么不引用按钮而是直接调用控制器是一个很好的设计。
另外,不需要 id——这是另一种分离视图和模型的 NG2 方式。
In Angular2:
If you don't want some visual feedback in the button, it's a good design to not reference the button but rather directly invoke the controller.
Also, the id isn't needed - another NG2 way of separating between the view and the model.
如果您还想禁用“发布到服务器”的输入按钮并执行 Js 脚本。
This in-case you want also diable the enter button from Posting to server and execute the Js script.
这个 onchange 尝试很接近,但行为不当浏览器后退然后前进(在 Safari 4.0.5 和 Firefox 3.6.3 上),所以最终我不会推荐它。
This onchange attempt is close, but misbehaves with respect to browser back then forward (on Safari 4.0.5 and Firefox 3.6.3), so ultimately, I wouldn't recommend it.
没有人注意到 html 属性“accesskey”已经可用一段时间了。
这是一种无需 JavaScript 的键盘快捷键方式。
MDN 上的 accesskey 属性快捷方式
打算这样使用。 html 属性本身就足够了,但是我们可以根据浏览器和操作系统更改占位符或其他指示符。 该脚本是一种未经测试的草稿方法来提供想法。 您可能想使用浏览器库检测器,例如小型 bowser
Nobody noticed the html attibute "accesskey" which is available since a while.
This is a no javascript way to keyboard shortcuts stuffs.
The accesskey attributes shortcuts on MDN
Intented to be used like this. The html attribute itself is enough, howewer we can change the placeholder or other indicator depending of the browser and os. The script is a untested scratch approach to give an idea. You may want to use a browser library detector like the tiny bowser
对于 jQuery mobile,我必须这样做:
For jQuery mobile, I had to do:
在处理事件时或在事件处理程序调用的函数中使用它。
它至少可以在 Internet Explorer 和 Opera 中运行。
Use it when handling the event or in the function your event handler calls.
It works in Internet Explorer and Opera at least.
要添加一个完全简单的 JavaScript 解决方案来解决@icedwater 的表单提交问题,这里有一个完整的解决方案,其中
表单
。小提琴: https://jsfiddle.net/rufwork/gm6h25th/1/
HTML
JavaScript
To add a completely plain JavaScript solution that addressed @icedwater's issue with form submission, here's a complete solution with
form
.Fiddle: https://jsfiddle.net/rufwork/gm6h25th/1/
HTML
JavaScript
对于那些可能喜欢简洁和现代 js 方法的人。
其中 input 是包含输入元素的变量。
For those who may like brevity and modern js approach.
where input is a variable containing your input element.
这是我的两分钱。 我正在开发一个适用于 Windows 8 的应用程序,并希望按钮在按下 Enter 按钮时注册单击事件。 我在 JS 中做这个。 我尝试了一些建议,但遇到了问题。 这很好用。
Heres my two cents. I am working on an app for Windows 8 and want the button to register a click event when I press the Enter button. I am doing this in JS. I tried a couple of suggestions, but had issues. This works just fine.
使用 jQuery 执行此操作:
使用普通 JavaScript 执行此操作:
To do it with jQuery:
To do it with normal JavaScript:
在 jQuery 中,您可以使用
event.which==13
。 如果您有表单
,则可以使用$('#formid').submit()
(将正确的事件侦听器添加到所述表单的提交中)。In jQuery, you can use
event.which==13
. If you have aform
, you could use$('#formid').submit()
(with the correct event listeners added to the submission of said form).如今,
change
事件就是这样!These day the
change
event is the way!我的可重用 Vanilla JS 解决方案。 因此您可以根据哪个元素/文本框处于活动状态来更改点击哪个按钮。
My reusable Vanilla JS solution. so you can change which button gets hit depending on what element/textbox is active.
您可以在 jQuery 中尝试以下代码。
You can try below code in jQuery.
我开发了自定义 javascript,只需添加类即可实现此功能
示例:
在这里查看 < a href="https://jsfiddle.net/RaviMakwana/k6zL1q9t/" rel="nofollow noreferrer">小提琴
-- 或 --
查看运行示例
https://stackoverflow.com/a/58010042/6631280
I have developed custom javascript to achieve this feature by just adding class
Example:
<button type="button" class="ctrl-p">Custom Print</button>
Here Check it out Fiddle
-- or --
check out running example
https://stackoverflow.com/a/58010042/6631280