繁体   English   中英

没有按住Ctrl键单击的多选框

[英]multi-select box without ctrl-click

我正在使用此stackoverflow帖子中编写的代码

如何避免使用Javascript在多选框中按住Ctrl键单击?

在许多其他文章中也建议不进行ctrl-click进行多项选择。

编码:

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

问题是代码不能在FireFox 31.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;
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM