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,
        },
    });
});

更多相关文章

  1. 具有嵌套元素的jQuery悬停事件
  2. Jquery隐藏()除一个类外所有具有特定类的元素
  3. jQuery -> 获取后代元素的三种方法
  4. JQuery操作<select>元素
  5. jQuery捕获被点击元素的ID
  6. Yii - 加载ajax表单元素的用户端验证
  7. 删除后重新插入后保留对元素的引用
  8. 通过调用返回参数的本地函数来构建Ajax Data部分
  9. jquery中获取相邻元素相关的命令:next()、prev()和siblings()

随机推荐

  1. AndroidStudio中的gradle脚本文件解读
  2. Android中的eventBus传值
  3. Android安全机制概述
  4. This Android(安卓)SDK requires Android
  5. Android SDK安装方法
  6. Android:shape的使用详解(1)
  7. Android Studio的Gradle文件方法说明
  8. Windows7下使用Eclipse搭建Cocos2dx+Andr
  9. Android核心入门分析
  10. 获取Android SDK 源代码并在Eclipse中关