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

时间:2017-06-05 07:57:11

标签: php jquery html jquery-select2

我正在编辑个人资料页面我对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');

我的代码只显示国家/地区名称。如何添加图标标志? output my code

1 个答案:

答案 0 :(得分:1)

我添加 templateSelection

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