Select2 drop-down for countries, with flags

后端 未结 4 1443
梦如初夏
梦如初夏 2020-12-29 13:46

Does somebody have an easy to use example of a country drop-down, with country flags, for Select2? I am about to implement one based on this suggestion, but I w

4条回答
  •  死守一世寂寞
    2020-12-29 14:22

    I was working on a similar problem and here is how I solve it.

    (function($) {
        $(function() {
            var isoCountries = [
                { id: 'AF', text: 'Afghanistan'},
                ...
            ];
            //Assuming you have a select element with name country
            // e.g. 
    
            $("[name='country']").select2({
                placeholder: "Select a country",
                data: isoCountries
            });
        });
    })(jQuery);
    

    I also have made a gist about it and following are the demos.

    • Demo 1
    • Demo 2 with flags based on official DOC of select2

提交回复
热议问题