如何使用 Javascript 避免在多选框中按住 Ctrl 键并单击?
我认为这将是一个简单的黑客攻击,但我现在已经搜索了几个小时,但找不到正确的搜索词。我想要一个普通的多重选择框 (
换句话说,我想要左键单击来切换光标下的 元素,而不更改任何其他元素。换句话说,我想要看起来像组合列表框但行为像一组复选框的东西。
有人能建议一个简单的方法来用 Javascript 做到这一点吗?谢谢。
I thought this would be a simple hack, but I've now been searching for hours and can't seen to find the right search term. I want to have an ordinary multiple select box (<select multiple="multiple">
) except I don't want the user to have to hold down the control key to make multiple selections.
In other words, I want a left click to toggle the <option>
element that's under the cursor without changing any of the others. In other other words, I want something that looks like a combo list box but behaves like a group of check boxes.
Can anybody suggest a simple way to do this in Javascript? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
检查这个小提琴: http://jsfiddle.net/xQqbR/1022/
你基本上需要覆盖每个
的
mousedown
事件,并在其中切换selected
属性。为简单起见,我在上面指定了“选项”作为选择器。您可以对其进行微调以匹配特定
Check this fiddle: http://jsfiddle.net/xQqbR/1022/
You basically need to override the
mousedown
event for each<option>
and toggle theselected
property there.For simplicity, I've given 'option' as the selector above. You can fine tune it to match
<option>s
under specific<select>
element(s). For ex:$('#mymultiselect option')
必须自己解决这个问题,并注意到错误行为,即简单拦截
mousedown
并设置属性,因此覆盖了 select 元素并且效果很好。jsFiddle: http://jsfiddle.net/51p7ocLw/
注意: 这段代码确实通过替换 DOM 中的 select 元素来修复错误行为。这有点激进,会破坏您可能附加到元素的事件处理程序。
Had to solve this problem myself and noticed the bugged behavior a simple interception of the
mousedown
and setting the attribute would have, so made a override of the select element and it works good.jsFiddle: http://jsfiddle.net/51p7ocLw/
Note: This code does fix buggy behavior by replacing the select element in the DOM. This is a bit agressive and will break event handlers you might have attached to the element.
techfoobar的答案是有问题的,如果您拖动鼠标,它会取消选择所有选项。
Sergio 的答案很有趣,但是克隆和删除绑定到下拉列表的事件并不是一件好事。
尝试这个答案。
注意:不适用于 Firefox,但在 Safari/Chrome/Opera 上完美运行。 (我没有在 IE 上测试它)
编辑(2020)
自我最初的回答以来 5 年后,我认为这里的最佳实践是用复选框替换下拉菜单。想想看,这就是复选框存在的主要原因,并且它可以很好地与 IE 和 IE 等旧浏览器配合使用。现代手机没有任何自定义 JS 来处理所有古怪的场景。
techfoobar's answer is buggy, it unselects all options if you drag the mouse.
Sergio's answer is interesting, but cloning and removing events-bound to a dropdown is not a nice thing.
Try this answer.
Note: Doesn't work on Firefox, but works perfectly on Safari/Chrome/Opera. (I didn't test it on IE)
EDIT (2020)
After 5 years since my original answer, I think best practice here is to replace the dropdown with checkboxes. Think about it, that's the main reason why checkboxes exist in the first place, and it works nicely with old browsers like IE & modern mobiles without any custom JS to handle all the wacky scenarios.
死灵术。
所选答案没有 jQuery。
另外,它错过了单击选项时设置焦点的机会,因为如果您编写 e.preventDefault,则必须自己执行此操作...
忘记聚焦会影响 CSS 样式,例如 bootstrap 等。
Necromancing.
The selected answer without jQuery.
Also, it missed setting the focus when an option is clicked, because you have to do this yourself, if you write e.preventDefault...
Forgetting to do focus would affect CSS-styling, e.g. bootstrap, etc.
可重用和纯JavaScript解决方案
Reusable and Pure JavaScript Solution
我今天遇到了同样的问题,通常建议是使用隐藏复选框列表并通过 css 模拟行为,这样更容易管理,但就我而言,我不想修改 html。
目前我仅使用谷歌浏览器测试了此代码,我不知道是否适用于其他浏览器,但它应该:
当然欢迎建议,但我还没有找到其他方法
I had same problem today, generally the advice is to use a list of hidden checkboxes and emulate the behavior via css, in this way is more easy to manage but in my case i don't want to modify html.
At the moment i've tested this code only with google chrome, i don't know if works with other browser but it should:
Of course suggestions are welcome but I have not found another way