gpt4 book ai didi

javascript - 如何检查选择列表是否是多个?选择2 JQuery、JavaScript

转载 作者:行者123 更新时间:2023-12-01 01:34:37 26 4
gpt4 key购买 nike

我有选择列表。在操作的情况下可以是多个或单个选择。

<select name="" id="cmb_employee"  class="js-example-basic-multiple" >
</select>

在 cmb_employee 中更改/我想确定选择列表是否为多个。我尝试过这个,但它适用于多个而不是单个

 $("#cmb_employee").on('change', function () {
var isMulti = document.getElementById('cmb_employee').multiple;
if(isMulti){
alert("is multi select");
}
else {
alert("is not");
}});

注1:我使用Select2

 $('.js-example-basic-multiple').select2({ width: '100%', cushion: 54 });

注2:将单个更改为多个

 $("#cmb_employee").select2({
multiple: true,
placeholder: "Seçin...",
});

注3:将多选改为单选

 $("#cmb_employee").select2({
multiple: false,
placeholder: "Seçin...",
});

最佳答案

您可以使用select2选项来获取多个的当前状态。$('#cmb_employee').data('select2').options.options.multiple

$('#cmb_employee').select2({
placeholder: 'Select a month',
multiple:true
});

$("#single").click(function(){
$('#cmb_employee').select2({
placeholder: 'Select a month',
multiple:false
});
})

$("#multiple").click(function(){
$('#cmb_employee').select2({
placeholder: 'Select a month',
multiple:true
});
})

$("#check").click(function(){
console.log("is dropdown is multiple? "+$('#cmb_employee').data('select2').options.options.multiple);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://rawgit.com/select2/select2/master/dist/css/select2.min.css" rel="stylesheet"/>
<script src="https://rawgit.com/select2/select2/master/dist/js/select2.js"></script>
<select id="cmb_employee" style="width: 300px">
<option value="JAN">January</option>
<option value="FEB">February</option>
<option value="MAR">March</option>
<option value="APR">April</option>
<option value="MAY">May</option>
<option value="JUN">June</option>
<option value="JUL">July</option>
<option value="AUG">August</option>
<option value="SEP">September</option>
<option value="OCT">October</option>
<option value="NOV">November</option>
<option value="DEC">December</option>
</select>

<button id="single">
Change to single
</button>
<button id="multiple">
Change to multiple
</button>
<button id="check">
Check dropdown is multiple or not
</button>

关于javascript - 如何检查选择列表是否是多个?选择2 JQuery、JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52942058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com