简体   繁体   中英

How can I set drop down values from JSON file

I am new to selectize and I would like to set the drop down list options of the tag input from a JSON file, right now the drop down shows what is on the input value.

{"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>

I finally found a way around this

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>';

}
}
                    });

您还可以在城市中循环并将每个选项作为子对象附加到DOM对象上。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM