admin管理员组

文章数量:1178571

I've tried to capture select2:clearing event

$("#my-select").on("select2:clearing", function (e) {
    console.log('cleared');
});

(jsfiddle)

but it is not fired. I've also tried other versions (like select2:removed etc., see another question with similar problem), but it also doesn't work.

I use select2 4.0.0-beta2.

I've tried to capture select2:clearing event

$("#my-select").on("select2:clearing", function (e) {
    console.log('cleared');
});

(jsfiddle)

but it is not fired. I've also tried other versions (like select2:removed etc., see another question with similar problem), but it also doesn't work.

I use select2 4.0.0-beta2.

Share Improve this question edited Jan 26, 2020 at 22:41 Brian Tompsett - 汤莱恩 5,87372 gold badges61 silver badges133 bronze badges asked Jan 21, 2015 at 11:00 LA_LA_ 20.4k60 gold badges176 silver badges316 bronze badges 2
  • Never used this library, but in the docs a dash is used in the event names instead of a colon, e.g. select2-clearing – Mario A Commented Jan 21, 2015 at 11:14
  • @MarioA, I've tried dashes, but the current doc (see select2.github.io/examples.html, Events section) says about colon. – LA_ Commented Jan 21, 2015 at 11:20
Add a comment  | 

4 Answers 4

Reset to default 19

This works:

$("#my-select").on("select2:unselecting", function(e) {

 });

The changelog of select2 4.0.0-beta2 states:

Removed events

select2-clearing - Use select2:unselecting instead

https://github.com/select2/select2/releases

$("#my-select").on("select2:select select2:unselecting", function(e) {
   //do something here
 });

According to the select2 event documentations, all of the unselect, unselecting, clear and clearing events will works !

But consider that usnselect or unselecting events should be used by single selection mode, and clear or clearing should be used by multi selection mode. So the clear or clearing will fires if all of selected items cleared or clearing.

The differences between unselect and unselecting, is that unselecting fires before removing selection so you can prevented it.

Hope that it helps.

本文标签: javascriptHow to capture event when allowClear option of select2 is chosenStack Overflow