admin管理员组

文章数量:1410737

I have severall select2 in my page. I have a specific ajax engine and can't change it for many reasons. i would like to fill each select2 dropdown when the end-user type in the field, and use home made javascript to fill the "options" in the select2.

I've tried many different things found on the net, and can't find a working syntax for that.

For instance :

      jQuery("customer").select2({
          query: function (options) {
              alert('ok');
              // my ajax call would be here
              xajax_getCustomers(stringFilled);
          }
      });

i've tried with "ajax:" and several other things, i can't find how to trigger a javascript function when something filled. Important : i should be able to get the string filled, in order to pass it to my ajax call

Thanks for your help

I have severall select2 in my page. I have a specific ajax engine and can't change it for many reasons. i would like to fill each select2 dropdown when the end-user type in the field, and use home made javascript to fill the "options" in the select2.

I've tried many different things found on the net, and can't find a working syntax for that.

For instance :

      jQuery("customer").select2({
          query: function (options) {
              alert('ok');
              // my ajax call would be here
              xajax_getCustomers(stringFilled);
          }
      });

i've tried with "ajax:" and several other things, i can't find how to trigger a javascript function when something filled. Important : i should be able to get the string filled, in order to pass it to my ajax call

Thanks for your help

Share Improve this question edited Feb 17, 2016 at 8:54 Vitaliy Terziev 6,7113 gold badges19 silver badges28 bronze badges asked Feb 17, 2016 at 8:35 Micbol12Micbol12 531 silver badge4 bronze badges 4
  • 1 select2.github.io/examples.html#data-ajax You don't need to make your custom function, select2 plugin es with this feature integrated. – Marcos Pérez Gude Commented Feb 17, 2016 at 8:43
  • I already saw those informations. But it's for a "standard" ajax call with JSON. this is not my case. i want to be able to call my own javascript function – Micbol12 Commented Feb 17, 2016 at 10:02
  • I add an answer with that. Tell me if it solves your problem – Marcos Pérez Gude Commented Feb 17, 2016 at 11:18
  • What version of select2 are you using? 3.5.x or 4.0.x? – Kevin Brown-Silva Commented Feb 18, 2016 at 12:19
Add a ment  | 

1 Answer 1

Reset to default 5

You have got this event:

  $(selector).on("select2-highlight", function(e) {
      console.log("highlighted val=" + e.val + " choice=" + e.choice.text);
  })

It's valid for a search event, so when select2-highlight event is fired means that user is searching for a string that you can manage with the e.val and e.choice.text values.

Unfortunatelly there's no strict search event, but you can bind the hided input text of the plugin with a on('keyup');

Something like this:

 $('input.select2-search__field').on('keyup', function() {
      alert($(this).val()); // it alerts with the string that the user is searching
 });

本文标签: Jquery select2 custom javascript function when searchingStack Overflow