如何在 jquery multiselect 中 select 和 deselect 组选项

How to select and deselect group of options in jquery multiselect

我知道我可以将 'checkAll''uncheckAll' 传递给我的多选实例,但我如何取消选择一组选项?

var deselectOptions = [1, 2, 3];

$('#my_select').multiselect('checkAll'); //except deselect options

试试这个来检查所有除了:

var deselectOptions = [1, 2, 3];

$('#my_select').multiselect('checkAll');

$('#my_select :selected').each(function () {
    if (deselectOptions.includes(parseInt($(this).val()))) {
        $(this).prop('selected', false);
    }
});

$('#my_select').multiselect('refresh');

或相反:

var selectOptions = [1, 2, 3];

//may not be required depending on use case
$('#my_select').multiselect('uncheckAll');

$('#my_select option').each(function () {
    if (selectOptions.includes(parseInt($(this).val()))) {
        $(this).prop('selected', true);
    }
});

$('#my_select').multiselect('refresh');