小编bey*_*yst的帖子

在select2中为选项添加图标

我试图在选项旁边放置类似于GitHub中的标志示例的图标:http://ivaynberg.github.io/select2/#templating,但需要图标基于他们所在的optgroup.我每个选项都会使用相同的图标,而不是仅仅在他们的optgroup中.

JS

$(function() {
  function format(ade) {
    if (!ade.id)
      return ade.text; // optgroup
    return "<i class='icon-info'></i>" + ade.text;
  }
  function format(bob) {
    if (!bob.id)
      return bob.text; // optgroup
    return "<i class='icon-user'></i>" + bob.text;
  }
  function format(jive) {
    if (!jive.id)
      return jive.text; // optgroup
    return "<i class='icon-exchange'></i>" + jive.text;
  }
  $("#source").select2({
    placeholder: "Get Started...",
    allowClear: true,
    minimumInputLength: 2,
    formatResult: format,
    formatSelection: format,
    escapeMarkup: function(m) {
      return m;
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

HTML

<select id="source" class="search-box">
  <option></option>
  <optgroup label="Book of Business"> …
Run Code Online (Sandbox Code Playgroud)

search jquery jquery-plugins jquery-select2

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

jquery ×1

jquery-plugins ×1

jquery-select2 ×1

search ×1