将参数传递给jQuery的select2 ajax调用
16lz
2021-01-22
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,
},
});
});
更多相关文章
- 具有嵌套元素的jQuery悬停事件
- Jquery隐藏()除一个类外所有具有特定类的元素
- jQuery -> 获取后代元素的三种方法
- JQuery操作<select>元素
- jQuery捕获被点击元素的ID
- Yii - 加载ajax表单元素的用户端验证
- 删除后重新插入后保留对元素的引用
- 通过调用返回参数的本地函数来构建Ajax Data部分
- jquery中获取相邻元素相关的命令:next()、prev()和siblings()