我正在尝试使用selectize.js库。在document.ready上,我进行了ajax调用并获取要添加到选择列表中的选项。
这是select标签的html
<select id="select-country-field" name="country-field-list[]" multiple class="demo-default" style="width:50%" placeholder="Select a state...">
<option value="">Select ...</option>
</select>
这是我如何在$ {document).ready上添加选项
var $select = $(document.getElementById('select-country-field')).selectize(options);
var selectize = $select[0].selectize;
selectize.addOption({value:1,text:'foo'});
selectize.addItem(1);
selectize.refreshOptions();
我看了问以下问题,但未能使其正常工作Selectize.js手动添加了一些项目
有什么帮助吗?
您可以将ajax调用移动到selectize加载方法中,如下所示:
$('#select-country-field').selectize({
valueField: 'country',
labelField: 'country',
searchField: 'country',
options: [],
load: function(query, callback) {
if (!query.length) return callback();
$.ajax({
url: 'http:// ajax-call-url',
type: 'GET',
dataType: 'json',
data: {
country: query,
},
error: function() {
callback();
},
success: function(res) {
callback(res);
}
});
}
});
“ http:// ajax-call-url”应返回如下数组:
[{ country: 'USA'}, {country: 'GB'}, {country: 'FR'}, ... ]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句