Jquery选项,键盘聚焦隐藏选项
我有一个带有一些选项的选择标签,其中一些选项是隐藏的,另一些则不是。 当我按键盘上的任意键时,焦点也会转到隐藏选项。
示例:
<select id='myselect'>
<options id='a'>a</options>
<options id='b' style ='display:none'>b</options>
<options id='c'>c</options>
<options id='b'>b</options>
</select>
上面我有一个有 4 个选项的选择,现在只有第一个、第三个和第四个选项可见,因为我将第二个选项隐藏起来。
现在,当我从键盘上按“b”时,它会转到隐藏选项,即我必须从键盘上按两次“b”才能到达第四个选项。
谁能告诉我如何解决这个问题?
当我按键盘上的任意键时,焦点应仅考虑非隐藏选项。
I have a select tag with some options, where some are hidden and some or not.
When I press any key from the keyboard, the focus goes to hidden options also.
Example:
<select id='myselect'>
<options id='a'>a</options>
<options id='b' style ='display:none'>b</options>
<options id='c'>c</options>
<options id='b'>b</options>
</select>
Above I have a select which has 4 options, now only first, third and fourth will be visible, because I have kept second as hidden.
Now when I press 'b' from the keyboard, it goes to the hidden option, i.e I have to press 2 times 'b' from the keyboard to reach 4th option.
Can anyone tell me how can solve this problem?
When I press any key from the keyboard, the focus should consider only the non-hidden options.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 HTML,您可以拥有类和 ID,我相信您知道,您可以在一个页面上拥有任意数量的类,但您不能在单个页面上拥有多个 ID,这就是您的问题所在。一个 HTML 页面绝不能有多个 ID。
这样做的原因是因为 ID 用于标识页面上的单个元素(即使它不可见,也不意味着它不存在。
您需要重命名选项 ID,以便它们都不同。
With HTML you have Classes and ID's as I'm sure you know, you can have as many classes on a page as you like, but you cannot have more than one ID on a single page, and this is where your problem lies. A HTML page must never have more than one ID.
The reason for this is because ID's are used to identify single elements on the page (Even if it's not visible, it doesn't mean it's not there.
You will need to rename your option ID's so they are all different.
即使所有标签都有唯一的 ID,隐藏的选项也会通过键盘选择。为了避免这种情况,该元素也必须被禁用。
在 jquery 1.6+ 中,这可以通过使用
.prop('disabled',false)
来实现,对于较低版本.attr('disabled','disabled')
我知道这个问题有点老了,但我也遇到过类似的情况,这个问题不断出现......
编辑:
隐藏选项标签似乎只在 Firefox 中有效。为了获得更好的兼容性,应该删除这些选项而不是隐藏它们。
Even if all tags have unique ids, hidden options get selected by keyboard. In order to avoid that, the element has to be disabled as well.
In jquery 1.6+ this can be achieved by using
.prop('disabled',false)
, for lower versions.attr('disabled','disabled')
I know the question is a little old, but I just had a similar situation and this question kept popping up...
Edit:
Hiding options tags seems to work only work in firefox. For better compatibility one should remove the options instead of hiding them.