Javascript 不按ctrl键单击的多选框

Javascript 不按ctrl键单击的多选框,javascript,jquery,firefox,multi-select,Javascript,Jquery,Firefox,Multi Select,我使用的代码是写在这篇文章 在许多其他文章中,也建议在不按ctrl键的情况下进行多次选择 守则: $('option').mousedown(function(e) { e.preventDefault(); $(this).prop('selected', !$(this).prop('selected')); return false; }); 问题是该代码在Firefox31.0上不起作用。 您可以使用以下链接进行尝试 有人知道解决这个问题的方法吗:)下面的代

我使用的代码是写在这篇文章

在许多其他文章中,也建议在不按ctrl键的情况下进行多次选择

守则:

$('option').mousedown(function(e) {
    e.preventDefault();
    $(this).prop('selected', !$(this).prop('selected'));
    return false;
});
问题是该代码在Firefox31.0上不起作用。 您可以使用以下链接进行尝试


有人知道解决这个问题的方法吗:)

下面的代码在firefox 31.0、IE 10和crome 36.0.1985.143中都可以使用。但如果同时使用CTRL键,则效果不佳

 $('select').bind("click", function (event, target) {

        event.preventDefault();
        var CurrentIndex = event.target.selectedIndex==undefined? $(event.target).index(): event.target.selectedIndex
        var CurrentOption = $("option:eq(" + CurrentIndex+ ")", $(this));
        if ($(CurrentOption).attr('data-selected') == undefined || $(CurrentOption).attr('data-selected') == 'false') {
            $(CurrentOption).attr('data-selected', true);
        }
        else {
            $(CurrentOption).prop('selected', false).attr('data-selected', false);
        }

        $("option", $(this)).not(CurrentOption).each(function (Index, OtherOption) {
            $(OtherOption).prop('selected', ($(OtherOption).attr('data-selected') == 'true') ? true : false);
        });
         return false;
    });

我是jqueryui的multiselect插件的忠实粉丝,这也允许人们进行筛选,并有一个“全选”选项供您发表评论。实际上,我最好不使用新的库来查找问题:)您是否尝试过使用鼠标滚动按钮点击选项?:D实际上不需要按CTRL键,只需使用鼠标左键即可:)我测试了这个。没有错误。它只是不起作用。你可以测试是否按下了ctrl或meta键。另外,我相信它是
.on
上的,不再是
.bind
谢谢你的评论,有一个简单的问题,但我不知道在哪里。如果“选择”以如下所示的某些选定选项开始,现在如果您尝试选择其他选项,它将始终清除已选定的选项。@user3584625我已使用选定的自定义属性数据跟踪其状态,因此您还需要维护该属性。因此,当您已经选择了选项时,请将该属性也设置为true。它在IE 11中不起作用。不能选择多个值