使用图标标志从数据库中设置选定值select2

时间:2022-11-27 19:22:51

i'm doing edit profile page i've problem with select2 for country. i'm using html and php. below is my code for jquery.

我正在编辑个人资料页面我对select2国家的问题。我正在使用HTML和PHP。下面是我的jquery代码。

function formatCountry (country) {

            if (!country.id) { return country.text; }
            var $country = $(
                '<span class="flag-icon flag-icon-'+ country.id.toLowerCase() +' flag-icon-squared"></span>' +
                '<span class="flag-text">'+ country.text+"</span>"
            );
            return $country;
        };


        $("[name='country']").select2({
            placeholder: "Select a country",
            templateResult: formatCountry,
            data: isoCountries

        });
        $("#country").val('<?php echo  $profile_fields[11]["value"];?>').trigger('change');

My code only display country name.how to add icon flag? output my code

我的代码只显示国家名称。如何添加图标标志?输出我的代码

1 个解决方案

#1


1  

i add templateSelection

我添加templateSelection

$("[name='country']").select2({ placeholder: "Select a country", templateResult: formatCountry, templateSelection: formatCountry, data: isoCountries, id: '<?php echo $profile_fields[11]["value"];?>' });

$( “[名称= '国家']”)SELECT2({占位符: “选择国家”,templateResult:formatCountry,templateSelection:formatCountry,数据:isoCountries,ID:?“ '}); 回声$>

#1


1  

i add templateSelection

我添加templateSelection

$("[name='country']").select2({ placeholder: "Select a country", templateResult: formatCountry, templateSelection: formatCountry, data: isoCountries, id: '<?php echo $profile_fields[11]["value"];?>' });

$( “[名称= '国家']”)SELECT2({占位符: “选择国家”,templateResult:formatCountry,templateSelection:formatCountry,数据:isoCountries,ID:?“ '}); 回声$>