IE9,聚焦于输入文本框,但按回车键将执行第一个输入按钮。我需要它停止
我有一个网络应用程序,人们可以在其中搜索内容。在 IE9 中,焦点位于搜索输入元素上,但第一个按钮突出显示,因此当有人按 Enter 时,它始终会执行第一个按钮。
我不希望人们能够按 Enter 键并执行第一个按钮。
原因是因为有些人倾向于在搜索的输入文本框中键入后按 Enter,即使它是基于 keyup 事件。
我尝试将其重新聚焦到搜索输入元素。我也尝试过模糊按钮。这似乎不起作用。
有谁知道这个问题的解决方案吗?
感谢您抽出时间。
编辑: 我不想劫持 Enter 按钮,因为如果有人实际上将焦点放在第一个按钮上并按 Enter 键怎么办?也许我可以检查一下?或者也许有更好的解决方案?
编辑2:在这里发现类似的问题: 阻止 IE 高亮显示第一个提交按钮一个表格 但这个问题的解决方案就是 ASP。
编辑3:
代码示例:
<div >
<label>ETTRN:<input type="text" id="search" maxlength="16" /></label>
<input type="submit" value="Generate CSV" id="generate_csv" />
</div>
I have a web app, where people can search for stuff. In IE9 the focus is on the search input element but the first button is highlighted so when someone press enter it would always execute the first button.
I don't want people to be able to press enter and execute the first button.
The reason is because some people tends to press enter after typing in the search's input text box even though it's base on keyup event.
I've try refocusing it to the search input element. I've also try blurring the button. It doesn't seem to work.
Does anyone know a solution to this?
Thank you for your time.
edit:
I don't want to highjack the enter button because what if someone actually tab the focus on to the first button and press enter? Maybe I can check for that? Or maybe there's a better solution?
Edit 2: Found a similar problem here:
Stopping IE from highlighting the first submit-button in a form
But the solution is for that question is ASP.
edit 3:
A code example:
<div >
<label>ETTRN:<input type="text" id="search" maxlength="16" /></label>
<input type="submit" value="Generate CSV" id="generate_csv" />
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将搜索框单独放入
这将(或至少应该)防止 IE(或任何浏览器)将第一个按钮视为 Enter 键的操作。
Put the search box inside a
<form>
all on its own.This will (or at least, should) prevent IE (or any browser) from treating the first button as the action for the Enter key.
我想出一个解决办法。
我只关心焦点何时位于用于搜索的文本输入元素(即搜索、开始日期和结束日期)。因此,如果按下某个键时这三个文本输入元素之一处于焦点,请检查它是否是 Enter 键 (13),如果是,则禁用默认值。
谢谢大家的帮助。
I figure out a solution.
I only care about when the focus is on the text input elements, that are used for searching (namely search, date-start, and date-end). So if one these three text input elements is in focus while a key is press, check if it's the Enter key (13), if it is then disable the default.
Thank you all for helping.
该问题与此处描述的文档类似:https://github。 com/aleen42/PersonalWiki/issues/32#issuecomment-489966824
如果你想解决问题,你可以使用改为
keypress
或keydown
事件:或者用空表单包装输入元素:
The problem is similar to what a document has described here: https://github.com/aleen42/PersonalWiki/issues/32#issuecomment-489966824
If you want to solve the problem, you can use
keypress
orkeydown
event instead:Or wrap the input element with a empty form: