Jquery S00LJU3LJU3LJU3LJU2LJG2LWmTnI4M0Ni4YNZQSni44NtfsltMxlJQWnSzmS40mUgIcGy00LJU2Cw0LJU2Lc0Nc0Nc0Nc0Lc0Lc0LJg1Nc0Nc0Nc0Nc0Nc2Lc2LdJg1Nc2Ld1Nc2Ld1Nc2Lc2Lc2Ld1Nc2Ld1Nc2Lc2Lc2Lc2Ld1Nc2Lc2Lc2Lc2Lc2Lc2Ld1Nc2Ld1Nc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Ld2L2.目前,中国还处于一个新的军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事(一); 背景尺寸:包含; 光标:指针; z指数:999999; } 选择1 选择2 选择3 选择4
来自的答案非常有效Jquery S00LJU3LJU3LJU3LJU2LJG2LWmTnI4M0Ni4YNZQSni44NtfsltMxlJQWnSzmS40mUgIcGy00LJU2Cw0LJU2Lc0Nc0Nc0Nc0Lc0Lc0LJg1Nc0Nc0Nc0Nc0Nc2Lc2LdJg1Nc2Ld1Nc2Ld1Nc2Lc2Lc2Ld1Nc2Ld1Nc2Lc2Lc2Lc2Ld1Nc2Lc2Lc2Lc2Lc2Lc2Ld1Nc2Ld1Nc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Lc2Ld2L2.目前,中国还处于一个新的军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事军事(一); 背景尺寸:包含; 光标:指针; z指数:999999; } 选择1 选择2 选择3 选择4,jquery,jquery-select2,Jquery,Jquery Select2,来自的答案非常有效 // Select all $('#select-id').select2('destroy').find('option').prop('selected', 'selected').end().select2(); // Unselect all $('#select-id').select2('destroy').find('option').prop('selected', false).end().select2(); $.fn.select2.amd.requ
// Select all
$('#select-id').select2('destroy').find('option').prop('selected', 'selected').end().select2();
// Unselect all
$('#select-id').select2('destroy').find('option').prop('selected', false).end().select2();
$.fn.select2.amd.require([
'选择2/utils',
“选择2/下拉列表”,
'选择2/下拉列表/附件'
],函数(Utils,下拉列表,附件){
函数SelectAll(){}
选择all.prototype.render=函数(装饰){
var$rendered=decorded.call(this);
var self=这个;
变量$selectAll=$(
“全选”
);
$rendered.find('.select2下拉列表').prepend($selectAll);
$selectAll.on('click',函数(e){
var$results=$rendered.find('.select2-results_u选项[aria selected=false]');
//获取所有未选择的结果
$results.each(函数(){
var$result=$(此);
//获取它的数据对象
var data=$result.data('data');
//触发选择事件
self.trigger('select'{
数据:数据
});
});
自触发(“关闭”);
});
退还已支付的美元;
};
$(“#选择id”)。选择2({
占位符:“选择工作日…”,
allowClear:是的,
下拉适配器:Utils.decoration(
装饰(
下拉列表,
附件
),
全选
)
});
});
源代码:看看我创建的这个JSFIDLE,确定这就是您搜索的所有答案
$(“.checkbox”)。单击(函数(){
如果($(“.checkbox”).is(“:checked”)){
$(this).parent().find('option').prop(“选定的”、“选定的”);
$(this.parent().find('option').trigger(“change”);
$(this).parent().find('option')。单击();
}否则{
$(this).parent().find('option').removeAttr(“选定的”、“选定的”);
$(this.parent().find('option').trigger(“change”);
}
});
$(“#按钮”)。单击(函数(){
警报($(“select”).val());
});
$(文档).ready(函数(){
$('.select2')。选择2({
closeOnSelect:false,
allowClear:错误
});
});代码>
沃尔沃汽车
萨博
欧宝
奥迪
全选
沃尔沃汽车
萨博
欧宝
奥迪
全选
select2中的全选自定义实现
函数RunSelect2(){
$(“#选择id”)。选择2({
allowClear:是的,
closeOnSelect:false,
}).on('select2:open',function(){
setTimeout(函数(){
$(“.select2-results\uuu option.select2-results\uu group”).bind(“单击”,selectAlllickHandler);
}, 0);
});
}
运行select2();
var selectAlllickHandler=function(){
$(“.select2-results\uuu option.select2-results\uuu group”)。取消绑定(“单击”,selectAlllickHandler);
$('select id')。select2('destroy')。find('option')。prop('selected','selected')。end();
运行select2();
};代码>
。选择2-results\u组
{
光标:指针!重要;
}
一个
二二
三三
四四
五五
六六
如果您需要此操作的复选框,只需简单地执行此操作即可
<select id="myselect" multiple="multiple">
<option value="level1">Level 1</option>
<option value="level2">Level 2</option>
<option value="level3">Level 3</option>
<option value="level4">Level 4</option>
</select>
<br/>
Select/Deselect <input type="checkbox" id="selectall"/>
2019年更新
要以编程方式为Select2控件选择选项/项,请使用jQuery.val()
方法
您还可以将数组传递给val
进行多项选择:
$('#mySelect2').val(['1', '2']);
$('#mySelect2').trigger('change');
改变
$result.data('data');
到
这行代码将选择所有选项
$('select.select-all-class-name').attr('selected', true).parent().trigger('change');
添加选择事件以适用于所有场景。当您选择并取消选择一个选项,然后单击selectAll时,上述行在场景中失败。最后取消选择的选项将不会被选中。为了解决这个问题,请添加下面的代码行
//Select options selected true for the selected option
$('#mySelect2').on('select2:selecting', function (e) {
$('select#mySelect2ID > option[value="'+e.params.args.data.id+'"]').attr('selected', true);
});
//DeSelect options selected to false for the option deselected
$('#mySelect2').on('select2:unselecting', function (e) {
$('select#mySelect2ID > option[value="'+e.params.args.data.id+'"]').attr('selected', false);
});
请参阅下面的代码
$('.select2').select2({
formatResult:function(object, container, query){
if(object.id=='all' || object.id=='clear')
return '<span style="color:#31708F;font-size:10px;"><i class="fa fa-chevron-right"></i> '+object.text+'</span>';
return object.text;
}
});
$('.select2').on("change", function(e) {
if($.inArray('all', e.val)===0){
var selected = [];
$(this).find("option").each(function(i,e){
if($(e).attr("value")=='all' || $(e).attr("value")=='clear')
return true;
selected[selected.length]=$(e).attr("value");
});
$(this).select2('val',selected);
}else if($.inArray('clear', e.val)===0){
$(this).select2('val','');
}
});
$('.select2')。选择2({
formatResult:函数(对象、容器、查询){
if(object.id=='all'| | object.id=='clear')
返回“”+对象。文本+“”;
返回object.text;
}
});
$('.select2')。打开(“更改”,函数(e){
如果($.inArray('all',e.val)==0){
所选var=[];
$(this).find(“option”).each(函数(即,e){
if($(e).attr(“值”)='all'| |$(e).attr(“值”)=='clear')
返回true;
选定的[selected.length]=$(e.attr(“值”);
});
$(this)。选择2('val',选中);
}否则如果($.inArray('clear',e.val)==0){
$(this)。选择2('val','');
}
});
参考来源:
$(文档).ready(函数(){
$(“#我的选择”).select2();
});
函数selectAll(){
$(“#我的选择>选项”).prop(“已选择”,true);
$(“我的选择”)。触发(“更改”);
}
函数取消全部(){
$(“#我的选择>选项”).prop(“已选择”,false);
$(“我的选择”)。触发(“更改”);
}
.button容器{
边缘底部:10px;
}
#我的选择{
宽度:200px;
}
全选
全部取消选择
苹果
香蕉
葡萄
菠萝
@kkudi问Github上的作者我该问作者什么?上面的代码根本不适合我。不知道为什么。我也对代码进行了调试。不过,我的数据是通过ajax加载的,不确定是否有任何区别。我不确定这是我要求的行为。我想要的是一次
// Select all
$('#select-id').select2('destroy').find('option').prop('selected', 'selected').end().select2();
// Unselect all
$('#select-id').select2('destroy').find('option').prop('selected', false).end().select2();
$.fn.select2.amd.require([
'select2/utils',
'select2/dropdown',
'select2/dropdown/attachBody'
], function (Utils, Dropdown, AttachBody) {
function SelectAll() { }
SelectAll.prototype.render = function (decorated) {
var $rendered = decorated.call(this);
var self = this;
var $selectAll = $(
'<button type="button" style="width: 100%; height: 3rem; text-align: left; color: blue; background: none; border: none; box-shadow: none">Select All</button>'
);
$rendered.find('.select2-dropdown').prepend($selectAll);
$selectAll.on('click', function (e) {
var $results = $rendered.find('.select2-results__option[aria-selected=false]');
// Get all results that aren't selected
$results.each(function () {
var $result = $(this);
// Get the data object for it
var data = $result.data('data');
// Trigger the select event
self.trigger('select', {
data: data
});
});
self.trigger('close');
});
return $rendered;
};
$("#select-id").select2({
placeholder: "Select weekdays...",
allowClear: true,
dropdownAdapter: Utils.Decorate(
Utils.Decorate(
Dropdown,
AttachBody
),
SelectAll
)
});
});
<select id="myselect" multiple="multiple">
<option value="level1">Level 1</option>
<option value="level2">Level 2</option>
<option value="level3">Level 3</option>
<option value="level4">Level 4</option>
</select>
<br/>
Select/Deselect <input type="checkbox" id="selectall"/>
$("#selectall").on('click',function(){
var checked = $("#selectall").prop("checked");
if(checked==true){
$("#myselect > option").prop("selected",true);
}else{
$("#myselect > option").prop("selected",false);
}
});
$('#mySelect2').val(['1', '2']);
$('#mySelect2').trigger('change');
$result.data('data');
Utils.GetData($result.get(0),'data')
$('select.select-all-class-name').attr('selected', true).parent().trigger('change');
//Select options selected true for the selected option
$('#mySelect2').on('select2:selecting', function (e) {
$('select#mySelect2ID > option[value="'+e.params.args.data.id+'"]').attr('selected', true);
});
//DeSelect options selected to false for the option deselected
$('#mySelect2').on('select2:unselecting', function (e) {
$('select#mySelect2ID > option[value="'+e.params.args.data.id+'"]').attr('selected', false);
});
$('.select2').select2({
formatResult:function(object, container, query){
if(object.id=='all' || object.id=='clear')
return '<span style="color:#31708F;font-size:10px;"><i class="fa fa-chevron-right"></i> '+object.text+'</span>';
return object.text;
}
});
$('.select2').on("change", function(e) {
if($.inArray('all', e.val)===0){
var selected = [];
$(this).find("option").each(function(i,e){
if($(e).attr("value")=='all' || $(e).attr("value")=='clear')
return true;
selected[selected.length]=$(e).attr("value");
});
$(this).select2('val',selected);
}else if($.inArray('clear', e.val)===0){
$(this).select2('val','');
}
});