在选择optgroup
中的元素时,我希望取消选择其他optgroup
中的元素。这是小提琴
https://jsfiddle.net/zfmr0std/
$("#block_house_suggest").select2().on("select2-selecting", function(e) {
var selected = $("#block_house_suggest [value='"+e.val+"']");
var opts = selected.parent().children();
var selected_array = [];
selected.prop('selected',true);
for (i = 0; i < opts.length; i++) {
if($(opts[i]).prop('selected')){
selected_array.push($(opts[i]).val());
}
}
$("#block_house_suggest").select2('data', selected_array);
});
答案 0 :(得分:0)
var selected = '';
$("#block_house_suggest").select2()
.on("select2-selecting", function(e) {
selected = e.val;
})
.on("change", function(e) {
var selected_list = e.val;
var opts = $("#block_house_suggest [value='"+selected+"']").parent().children();
var opts_array = [];
for (i = 0; i < opts.length; i++) {
opts_array.push($(opts[i]).val());
}
selected_list = selected_list.filter(function(n) {
return opts_array.indexOf(n) != -1;
});
$("#block_house_suggest").select2('val', selected_list);
e.preventDefault();
});