将参数传递给jQuery的选择2 Ajax调用 [英] passing parameters to jQuery's select2 ajax call

查看:131
本文介绍了将参数传递给jQuery的选择2 Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图一个额外的参数传递给内选择2 Ajax调用:

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

  $(".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};
        }
    }
  });

其实我是想另一个参数传递给Ajax调用...元素本身的id

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

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

然而

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

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

推荐答案

假设有带班自动SUG ,你可以尝试这样的事情多的元素:

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

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

这篇关于将参数传递给jQuery的选择2 Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆