Jquery引导程序多选optgroup选择

时间:2014-01-17 16:47:17

标签: jquery optgroup

我正在使用Jquery bootstrap multiselect插件,它似乎非常好并且满足了我需要的大部分要求。

我想要的唯一附加功能是向OptGroup添加复选框,因此如果用户想要选择完整的组,他们可以通过单击选择全部。

任何帮助表示感谢。

http://davidstutz.github.io/bootstrap-multiselect/#examples

enter image description here

5 个答案:

答案 0 :(得分:5)

无需执行任何代码即可达到您的要求。

此插件中有一个名为enableClickableOptGroups的选项,用于选择optgroup的所有选项

http://davidstutz.github.io/bootstrap-multiselect/#configuration-options-enableClickableOptGroups

答案 1 :(得分:4)

尝试这样的事情:

$('.multiselect-group').before('<input type="checkbox" />');
$(document).on('click', '.multiselect-group', function(event) {
    var checkAll = true;
    var $opts = $(this).parent().nextUntil(':has(.multiselect-group)'); 
    var $inactive = $opts.filter(':not(.active)'); 
    var $toggleMe = $inactive;
    if ($inactive.length == 0) { 
        $toggleMe = $opts;
        checkAll = false;
    }
    $toggleMe.find('input').click();
    $(this).parent().find('input').attr('checked', checkAll);
    event.preventDefault();
});

答案 2 :(得分:4)

您可以使用event.stopPropagation();

答案 3 :(得分:1)

检查选项组然后取消选中后,如果我想再次检查它,它会检查该组中的所有选项,但该组仍然未选中。

为了在多次重试后将选项组正确显示为选中/取消选中,请替换:

$(this).parent().find('input').**attr**('checked', checkAll);

$(this).parent().find('input').**prop**('checked', checkAll);

答案 4 :(得分:0)

在您的代码中仅将这一行代码

enableClickableOptGroups:是

这将启用选择组的选项

http://davidstutz.de/bootstrap-multiselect/#getting-started