2017-06-05 30 views
2

我正在做编辑配置文件页面我有select2国家的问题。我使用HTML和PHP。下面是我的jQuery代码。设置选定值select2从数据库与图标标志

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

我的代码只显示country name.how添加图标标志? output my code

+0

'templateResult:formatCountry'是下拉。 'templateSelection:formatCountry'会为选定的值使用相同的函数,我相信。 – apokryfos

+0

哦,是的,它的工作原理!谢谢@apokryfos – styles

回答

1

我添加templateSelection

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