我有以下javascript / jQuery从AJAX中提取数据。这工作正常,但我似乎无法从下拉列表中选择一个选项?谁能解释我做错了什么..
这与id ..
有关$(".js-data-example-ajax").select2({
ajax: {
url: "/admin/generator/teams",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
return {
results: data.items,
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
id: function(data){ return data._id; },
});
答案 0 :(得分:0)
首先,您应确保processResults
函数返回具有id
和text
属性的对象数组,如下所示:
{ id: '1', text: 'Option 1' }
然后加载选项后,您可以通过调用:
来设置值$(".js-data-example-ajax").val('1').trigger('change');