1、数据来源于静态数据
$(".test").select2({
width: "100%",
minimumResultsForSearch: -1,
data:[{id:1,name:1},{id:2,name:2},
{id:3,name:3},{id:4,name:4},
{id:5,name:5}],
formatSelection: function(item){
return item.name;
},
formatResult: function(item){
return item.name;
}
});
赋值
$(".test").val(1).trigger("change");
2、数据来源ajax请求
$(".test").select2({
width: "100%",
multiple: false,
placeholder: "请选择...",
ajax: {
url: "url",
type: "post",
dataType: "json",
data:function(term, page){
return {
start: (page - 1) * 10,
length: 10
};
},
results:function(response, page, query){
}
},
formatResult: this.proxy(function(item){
return item.name;
}),
formatSelection: this.proxy(function(item){
return item.name;
}),
id: this.proxy(function(item){
return item.value;
})
})
//赋值
$(".test").select2("data",{id:'',name:''})