我是新手,我想设置从JSON文件输入的标签的下拉列表选项,现在该下拉列表显示输入值上的内容。
{"California":["Los Angeles","San Diego","San Jose","San Francisco","Fresno","Sacramento"]}
<input type="text" id="myCity" class="form-control mb-4" placeholder="City" value="San Diego, San Francisco">
<script class="show">
$('#myCity').selectize({
delimiter: ',',
persist: false,
maxItems: 5,
plugins: ['remove_button'],
create: function(input) {
return {
value: input,
text: input
}
}
});
</script>
答案 0 :(得分:0)
我终于找到了解决方法
var data = ["Los Angeles", "San Diego", "San Jose", "San Francisco", "Fresno", "Sacramento"];
var cities = data.map(function(x) { return { city: x }; });
$('#input-tags').selectize({
delimiter: ',',
persist: false,
maxItems: 5,
plugins: ['remove_button'],
valueField: 'city',
labelField: 'city',
searchField: 'city',
options: cities,
create:true,
render: {
option: function(item, escape) {
return '<div>' +
'<span class="name">' + escape(item.city) + '</span>' +
'</div>';
}
}
});
答案 1 :(得分:0)
您还可以在城市中循环播放,并将每个选项作为子项附加在DOM对象上。