javascript - How can I set maximumSelectionLength while closeOnSelect is false in Select2? -
i have following code:
$("#select2").select2({ closeonselect: false, maximumselectionlength: 5, ... // have ajax implemented here });
when have closeonselect
false
, have maximumselectionlength
set, i'm able select more 5 items without problem. when click away dropdown while having more 5 items selected , try add another, message "you can select 5 items".
is there workarounds have these 2 properties work without clashing?
i'm using select2 4.0.1.
update:
i took pratikwebdev's advise , added following code:
$("#select2").select2({ closeonselect: false, maximumselectionlength: 5, ... // have ajax implemented here }).on("change", function(e) { if(e.target.length == 5) { $("#select2").select2({closeonselect: true, maximumselectionlength: 5, }); } });
this close dropdown once select 5 items , maximumselectionlength
property work properly. closeonselect
true each time select something, dropdown close.
is there way toggle closeonselect
?
this should work fine.
$("#select2").change(function(){ var ele = $(this); if(ele.val().length==5) { ele.select2('close'); } });
Comments
Post a Comment