问题描述
我在页面上有一些文本框,我想在用户按 Enter 键时单击一个链接.
I have some textboxes on a page and I want to click a link when the user presses enter in any of them.
我可以使用 javascript 轻松捕获回车按钮(通过在 event.keyCode
和 event.which
中查找 13),但是当浏览器自动完成时我遇到了问题功能开始并建议用户可能想要输入的内容.我们发现用户经常按回车键接受浏览器的建议,而不是选项卡.当用户立即点击链接时,这会让用户感到困惑,而他们仍然打算在其他一些字段中输入文本.
I can easily trap the enter button using javascript (by looking for 13 in event.keyCode
and event.which
), but I hit an issue when the browser's autocomplete feature kicks in and suggests what the user might want to type. We're finding the users often press enter to accept the browser's suggestion, rather than tab. This confuses the users as the link is clicked immediately, whereas they still intended to enter text into some of the other fields.
我知道在这里使用表单和提交按钮会更好,但由于种种原因这不切实际.
I know it would be better to use a form and a submit button here, but for various reasons that's not practical.
我使用的是 jQuery,所以请随时提供 jQuery 解决方案.
I'm using jQuery, so feel free to offer jQuery solutions.
推荐答案
如果用户选择自动完成文本右侧的一个,他们总是必须按下向下键,为什么不在按下时设置一个变量键,然后如果他们按下回车键,则检查变量.如果设置了变量,则不要做链接点击功能,否则照常进行.
User always have to press the down key if they choose to select one of the auto complete text right, why not set a variable to something when they press the down key, and then if they do the enter press afterwards you check the variable. You should not do the link click function if the variable is set, otherwise do it as normal.
这篇关于捕获回车键,但在选择浏览器的自动完成建议时不会捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!