[英]Listing with select2 ajax
I am doing a select box listing with select2 ajax. 我正在用select2 ajax做一个选择框清单。 I can list the data from the server but it is not listed in the selectbox.
我可以列出服务器中的数据,但未在选择框中列出。 How can I solve this problem?
我怎么解决这个问题?
HTML 的HTML
<input type="hidden" class="js-data-example-ajax form-control" />
jQuery jQuery的
$(".js-data-example-ajax").select2({
minimumInputLength: 3,
ajax: {
url: "/Contacts/Test1",
dataType: 'json',
delay: 250,
data: function (params) {
return {
"temp": params,
};
},
results: function (data) {
console.log(data)
debugger;
var parsed = [];
try {
parsed = $.map(data.data, function (item) {
console.log(item)
return {
ID: item.ID,
ADI: item.ADI
}
}).value();
} catch (e) {
}
console.log(parsed);
return {
results: parsed
};
},
cache: false
}
});
if you getting the array as ajax response then do iterate the response and add it in the option and finally append it to the select box. 如果您将数组作为ajax响应获取,则请对该响应进行迭代,并将其添加到选项中,最后将其附加到选择框。 eg html:
例如html:
<select class="test-select"></select>
eg: js 例如:js
for(var i=0;i<response.length;i++) {
var optionData = $("<option>"'+ response[i].id +'"</option>");
$('.test-select').append(optionData);
}
here create the option using jquery and append each option to the select 在这里使用jquery创建选项并将每个选项附加到选择
try it... 试试吧...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.