我正在尝试设置select2单选下拉列表的默认值:http : //ivaynberg.github.io/select2/。
我有select2下拉列表工作。但是我不确定如何设置默认值。例如,说值列表为-test1,test2,test3。如何将“ test1”设置为单选下拉列表的默认值。
$(function(){
var testData = [{id:0, text:"test1"}, {id:1, text:"test2"}, {id:2, text:"test3"}];
$('#select2customdata').select2({placeholder:"Search Stuff", data: {results: testData, text:'text'}});
$('#select2customdata').select2().select2("val", "test1");
});
和html有:
<input type="hidden" id="select2customdata" style="width:300px"> </input>
如您所见,我正在使用select2(“ val”,“ some-val”)设置默认值。它对我不起作用。
编辑:我想出了解决方案,如果使用val,必须在选项中有initselection。其他解决方案是使用:
$('#select2customdata').select2("data", {"id": id_val, "text": text_val})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句