【问题标题】:set selected value select2 from database with icon flags使用图标标志从数据库中设置选定值 select2
【发布时间】:2017-06-05 07:57:11
【问题描述】:

我正在编辑个人资料页面,我对国家/地区的 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

【问题讨论】:

  • templateResult: formatCountry 用于下拉菜单。我相信templateSelection: formatCountry 也会对所选值使用相同的函数。
  • 哦,是的,它有效!谢谢@apokryfos

标签: php jquery html jquery-select2


【解决方案1】:

我添加 templateSelection

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多