Select2-select event does not fire

I am using Select2 on my website and I am trying to use the select2-selecting event, but I am not quitting. I also use Backbone.js in the application, so the first thing I tried was to add select2-selecting to my events object:

 // 'change .city':'cityChanged' 'select2-selecting .city':'cityChanged' 

Note that the change event is commented out - this change event works correctly. In the documentation for Select2, the select2-selecting event is placed directly on the object, and not like this:

 $('.city').select2().on('select2-selecting', function(e){ console.log('here'); }); 

instead, it is intended to be used as follows:

 $('.city').on('select2-selecting', function(e){ console.log('here'); }); 

I also tried to add an event in both of these ways, but the event did not fire (I checked and the element was created on the DOM before , I added events).

When I add an event in the first method with Backbone.js , the event is displayed in the event listeners in the chrome debug console - it just isn't fired. Does anyone have an idea of ​​what is going on?

+4
source share
3 answers

what version of select2 are you using?

I had the same problem until I realized that I was using version 3.3, where this select2-selecting event does not exist.

This was included in version 3.4.

+14
source

In previous versions, there was a change and where it changes its name:

  • select2-close now select2: close
  • select2-open now select2: open
  • select2-opening is now select2: opening
  • select2-select now select2: choosing
  • select2-remove is now select2: removed
  • select2-remove is now select2: unselecting
+9
source

In even older versions, select2-deleted and select2-remove , listed on @ santi-iglesias ", the answer does not exist . Instead, you are" deleted . "Also, to get the optioin value, use event.val .

So you can do something like this:

 $('.select').on('select2-selecting removed', function(evt) { var value = evt.val; //do something with this }); 

Tested on v3.4.3.

0
source

Source: https://habr.com/ru/post/1490501/


All Articles