3

我的页面中有多个 select2。我有一个特定的 ajax 引擎,由于很多原因无法更改它。我想在最终用户在字段中键入时填写每个 select2 下拉列表,并使用自制的 javascript 填写 select2 中的“选项”。

我已经尝试了许多在网上找到的不同的东西,但找不到适用的语法。

例如 :

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

我已经尝试过“ajax:”和其他一些东西,我找不到填充东西时如何触发 javascript 函数。重要提示:我应该能够填充字符串,以便将其传递给我的 ajax 调用

谢谢你的帮助

4

1 回答 1

5

你有这个事件:

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

它对搜索事件有效,因此当select2-highlight事件被触发时,意味着用户正在搜索您可以使用e.valande.choice.text值管理的字符串。

不幸的是,没有严格的搜索事件,但是您可以将插件的隐藏输入文本与on('keyup');

像这样的东西:

 $('input.select2-search__field').on('keyup', function() {
      alert($(this).val()); // it alerts with the string that the user is searching
 });
于 2016-02-17T10:56:01.527 回答