admin管理员组文章数量:1391792
I have a multiple select element with select2 v 3.5.2 plugin.
I need to be sure that the specific option value -1
will be not selected with other values.
I tried this solution:
$('#mysel').on("change",function(e){
if($("#mysel option[value=-1]").is(":selected") && $("#mysel option[value!=-1]:selected").length>0){
$("#mysel").val(-1).trigger('change.select2');
}
});
But it doesn't work, it change the value in html but select2 container is not updated. I also tried to switch the trigger to .trigger('change')
but it didn't work.
I tried to run a change with a setTimeout()
and it works... so I guess there is an issue on the event triggering the same event. Is there a solution other than not-so-elegant setTimeout update?
I have a multiple select element with select2 v 3.5.2 plugin.
I need to be sure that the specific option value -1
will be not selected with other values.
I tried this solution:
$('#mysel').on("change",function(e){
if($("#mysel option[value=-1]").is(":selected") && $("#mysel option[value!=-1]:selected").length>0){
$("#mysel").val(-1).trigger('change.select2');
}
});
But it doesn't work, it change the value in html but select2 container is not updated. I also tried to switch the trigger to .trigger('change')
but it didn't work.
I tried to run a change with a setTimeout()
and it works... so I guess there is an issue on the event triggering the same event. Is there a solution other than not-so-elegant setTimeout update?
1 Answer
Reset to default 1Instead of change
event use select2-close
event. You don't have to use the setTimeout
here. Try the below snippet.
$("#mysel").select2();
$("#mysel").on("select2-close", function (e) {
if($("#mysel option[value=-1]").is(":selected") && $("#mysel option[value!=-1]:selected").length>0){
let values = $("#mysel").val().filter(value => value != -1);
$("#mysel").val(values).trigger('change');
}
});
<link href="https://cdnjs.cloudflare/ajax/libs/select2/3.5.2/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare/ajax/libs/select2/3.5.2/select2.min.js"></script>
<select multiple id="mysel" style="width:300px">
<option value="-1">None</option>
<option value="AL">Alabama</option>
<option value="Az">Arizona</option>
<option value="Ca">California</option>
<option value="Co">Colorado</option>
<option value="Fl">Florida</option>
</select>
Note that in the recent versions of the library, the event name select2-close
has been renamed to select2:close
.
本文标签: javascriptHow to control select2 selection on change eventStack Overflow
版权声明:本文标题:javascript - How to control select2 selection on change event? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744764119a2623931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
return false
after your .trigger line. This will then "cancel"/"abort" the first event so that the second event can be applied. – fdomn-m Commented Mar 12 at 9:11.trigger("change")
tells select2 that it has been changed and to update the underlying select. It's not to force a change on the select2 drop down - that should be what.val(..)
is doing - but looks like as it's in the middle of an event, the actual change is after that event. Hence cancelling the outer/first event means it won't get re-updated. – fdomn-m Commented Mar 12 at 9:31