如何避免使用 Javascript 在多选框中按 ctrl-click 的需要? [英] How to avoid the need for ctrl-click in a multi-select box using Javascript?

查看:21
本文介绍了如何避免使用 Javascript 在多选框中按 ctrl-click 的需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这将是一个简单的技巧,但我现在已经搜索了几个小时,但找不到正确的搜索词.我想要一个普通的多选框 (<select multiple="multiple">) 除非我不希望用户必须按住控制键才能进行多项选择.

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.

有人可以建议一种在 Javascript 中执行此操作的简单方法吗?谢谢.

Can anybody suggest a simple way to do this in Javascript? Thanks.

推荐答案

检查这个小提琴:http://jsfiddle.net/xQqbR/1022/

您基本上需要为每个 覆盖 mousedown 事件并在那里切换 selected 属性.

You basically need to override the mousedown event for each <option> and toggle the selected property there.

$('option').mousedown(function(e) {
    e.preventDefault();
    $(this).prop('selected', !$(this).prop('selected'));
    return false;
});

为简单起见,我将 'option' 作为上面的选择器.您可以微调它以匹配特定

发送“验证码”获取 | 15天全站免登陆