将参数传递给jQuery的select2 ajax调用

时间:2022-12-08 20:15:56

I'm attempting to pass an extra parameter to an ajax call within select2:

我正在尝试将一个额外的参数传递给select2中的ajax调用:

  $(".auto-sug").select2({
    width:'element',
    minimumInputLength:2,
    ajax: {
        url: "/action/get-custom.php",
        data: function (term, page) {
          return {
              q: term, // search term
              page_limit: 10
          };
        },
        results: function (data, page) {
            return {results: data.stuff};
        }
    }
  });

I actually want to pass another parameter to the ajax call... the id of the element itself

我实际上想要将另一个参数传递给ajax调用...元素本身的id

<input type="text" class="auto-sug" name="custom" id="3383" />

however, I'm unable to figure out how to actually access the id of the element (3383) or any other value on the page.

但是,我无法弄清楚如何实际访问元素的id(3383)或页面上的任何其他值。

3 个解决方案

#1


11  

Assuming there are multiple elements with class auto-sug, you could try something like this:

假设有多个元素具有类auto-sug,你可以尝试这样的事情:

$(".auto-sug").each(function() {
    var thisId = this.id;
    $(this).select2({
        ...
        ajax: {
            ...
            id: thisId,
        },
    });
});

#2


4  

You should pass that extra parameter inside the data function instead of the root ajax, for it to execute each time you make a request:

您应该在数据函数内传递该额外参数而不是根ajax,以便在每次发出请求时执行:

ajax: {
    url: "/action/get-custom.php",
    data: function (term, page) {
      return {
          q: term, // search term
          anotherParm: whatEverValue, //Get your value from other elements using Query, for example.
          page_limit: 10
      };

Then for getting the id for the current select2 you could replace whateverValue with $(this).data(key)

然后,为了获取当前select2的id,你可以用$(this)替换whateverValue .data(key)

#3


0  

You can add a new params here.

你可以在这里添加一个新参数。

 data: function (params) {
        ultimaConsulta = params.term;
        localidad = $("#idOcultoLocalidad").val(); //this is the anotherParm
        return {
            criterio: params.term, // search term
            criterio2: localidad,
        };
    },

#1


11  

Assuming there are multiple elements with class auto-sug, you could try something like this:

假设有多个元素具有类auto-sug,你可以尝试这样的事情:

$(".auto-sug").each(function() {
    var thisId = this.id;
    $(this).select2({
        ...
        ajax: {
            ...
            id: thisId,
        },
    });
});

#2


4  

You should pass that extra parameter inside the data function instead of the root ajax, for it to execute each time you make a request:

您应该在数据函数内传递该额外参数而不是根ajax,以便在每次发出请求时执行:

ajax: {
    url: "/action/get-custom.php",
    data: function (term, page) {
      return {
          q: term, // search term
          anotherParm: whatEverValue, //Get your value from other elements using Query, for example.
          page_limit: 10
      };

Then for getting the id for the current select2 you could replace whateverValue with $(this).data(key)

然后,为了获取当前select2的id,你可以用$(this)替换whateverValue .data(key)

#3


0  

You can add a new params here.

你可以在这里添加一个新参数。

 data: function (params) {
        ultimaConsulta = params.term;
        localidad = $("#idOcultoLocalidad").val(); //this is the anotherParm
        return {
            criterio: params.term, // search term
            criterio2: localidad,
        };
    },