【发布时间】:2015-08-28 06:02:30
【问题描述】:
我需要知道我的 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>
如您所见,我需要访问定义要随 ajax 请求发送的参数的函数内部的数据属性(或 id)。
【问题讨论】:
-
愿意分享您的代码和尝试吗?
-
我还没有尝试过任何东西,因为我找不到任何可以告诉我尝试什么的东西。代码真的很基础,至少在与获取信息直接相关的方面。这是一个要点:gist.github.com/Gremlyn/75cdd1a3aa6457efe477
标签: jquery-select2