Jquery bootstrap 多选 optgroup 选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21191304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:28:24  来源:igfitidea点击:

Jquery bootstrap multiselect optgroup selection

jqueryoptgroup

提问by d-man

I am using Jquery bootstrap multiselect plugin which seems pretty good and fulfill most of the requirements which i needed.

我正在使用 Jquery bootstrap 多选插件,它看起来很不错,可以满足我需要的大部分要求。

The only additional functionality i want is to add checkbox to OptGroup so if user wants to select complete group they can select all by clicking it.

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

any help appreciated.

任何帮助表示赞赏。

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

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

enter image description here

在此处输入图片说明

回答by AsgarAli

No need to do any code to achieve your requirement.

无需执行任何代码即可实现您的要求。

There is an option called enableClickableOptGroupsin this plugin to select all options of optgroup

enableClickableOptGroups此插件中有一个选项可以选择所有选项optgroup

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

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

回答by PatrickDelancy

Try something like this:

尝试这样的事情:

$('.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();
});

回答by naxxateux

You can use event.stopPropagation();

您可以使用 event.stopPropagation();

回答by Andreea Hash

After I checked an option group and then unchecked it, if I wanted to check it again, it would check all options from the group, but the group remained unchecked.

在我选中一个选项组然后取消选中它之后,如果我想再次选中它,它会选中该组中的所有选项,但该组保持未选中状态。

In order for the option group to be properly displayed as checked/unchecked after several retries, replace:

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

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

with

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

回答by Junip Dewan

in your code put this line of code only

在您的代码中只放置这行代码

enableClickableOptGroups: true

enableClickableOptGroups: 真

this will enable the option to select group

这将启用选择组的选项

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

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