本文介绍了jQuery Select2-如何选择所有选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用jQuery select2 multi select下拉列表.我需要从代码的下拉列表中选择所有选项.基本上有一个全选"复选框必须在其上实现此功能,我想从此复选框中选择/取消选择选项.
I'm using jQuery select2 multi select dropdown. I need to select all options in a dropdown from code.Basically there is a Select All checkbox on which this functionality has to be implemented, I want to select/deselect options from this checkbox.
推荐答案
在github上的线程中有描述.引用( https://github.com/ivaynberg/select2/issues/195#issuecomment -13489140 由 MortadaAK )创建,它使您可以选择ctrl + a上的所有内容
There is a description in thread on github. Quoting (https://github.com/ivaynberg/select2/issues/195#issuecomment-13489140 by MortadaAK) which allows you to select everything on ctrl+a
$(document).on("keypress",".select2-input",function(event){
if (event.ctrlKey || event.metaKey) {
var id =$(this).parents("div[class*='select2-container']").attr("id").replace("s2id_","");
var element =$("#"+id);
if (event.which == 97){
var selected = [];
element.find("option").each(function(i,e){
selected[selected.length]=$(e).attr("value");
});
element.select2("val", selected);
} else if (event.which == 100){
element.select2("val", "");
}
}
});
这篇关于jQuery Select2-如何选择所有选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!