我需要知道我的select2 UI所基于的元素,以便可以从数据属性中获取数据。也许我在文档中没有找到它,但是我似乎无法弄清楚如何获得它。
这是我的代码设置的基础知识:
<form>
<!--SNIP-->
<select class="select-records" data-index=0>
<option>Choose some records</option>
</select>
<!--SNIP-->
</form>
<script>
$('.select-records').select2({
ajax: {
url: "/ajax/records",
dataType: 'json',
delay: 250,
data: function (params) {
// Need to get the data-index of the base <select> to process/pass along more info here
return {
q: params.term,
page: params.page
}
},
processResults: function(data, page) {
return {
results: data.items
}
},
cache: true
},
minimumInputLength: 3
});
</script>
如您所见,我需要访问函数内的data属性(或id),该函数定义要与ajax请求一起发送的参数。
假设您要为select元素添加到ajax查询...&ui = select1中。
通过以下方式定义您的选择: <select data-ajax--id='select1' class="select-records" data-index=0>
您的ajax的数据功能现在应该是:
函数(参数){返回{q:params.term,ui:this.id,页面:params.page}}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句