2017-07-28 163 views

回答

1

您可以使用選擇二的templateSelection選項和一些自定義屬性選項標籤

HTML:

<select id="long" class="js-example-basic-multiple" multiple="multiple"> 
     <option data-custom="" value="all">All </option> 
     <option data-custom="Lorem ipsum" value="#FF0000">Lorem ipsum dolor sit amet consectetur adipiscing elit</option> 
     <option data-custom="Sed do" value="#00FF00">Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</option> 
     <option data-custom="Ut enim" value="#0000FF">Ut enim ad minim veniam quis nostrud exercitation</option> 
     <option data-custom="Duis aute" value="#FFFF00">Duis aute irure dolor in reprehenderit in voluptate velit esse</option> 
    </select> 

的Javascript:

jQuery(function() { 
      jQuery('#long').select2({ 
       templateSelection: function (selection) { 
        return jQuery(selection.element).data('custom'); 
       } 
      }); 
     }); 

https://jsfiddle.net/rijokpaul/phz9e5cs/4/