我是AJAX的新手,我正在尝试用select2从AJAX请求中获取结果。
这是我的HTML:
<select class="js-example-basic-single" name="Airport"><option disabled selected>Airport</option></select>这是我的select2调用:
$(function() {
initAirportList = function(){
$(".js-example-basic-single").select2({
// minimumInputLength: 2,
ajax: {
dataType: 'json',
type: "GET",
url: 'data',
delay: 250,
}
,});
}
initAirportList();
});JSON数据在这里:
[
{id:'ADL',text:'Adelaide, Australia, ADL'},
{id:'MEL',text:'Melbourne, Australia, MEL'},
{id:'PER',text:'Perth, Australia, PER'},
{id:'SYD',text:'Sydney, Australia, SYD'}
]我应该在select2 start的代码中添加什么来显示结果?
谢谢!
发布于 2017-03-01 15:58:30
首先看一下文档。描述了如何从远程资源加载数据。
https://select2.github.io/examples.html#data-ajax
和
https://select2.github.io/options.html#ajax
我认为,您在初始化过程中遗漏了一些选项。您必须在ajax请求中添加data选项:
ajax: {
dataType: 'json',
type: "GET",
url: 'data',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
}https://stackoverflow.com/questions/42523564
复制相似问题