【问题标题】:How to set placeholder first in Select2如何在Select2中首先设置占位符
【发布时间】:2015-09-15 13:40:09
【问题描述】:

如何先设置占位符而不是 Select2 中的默认值

我这里有这段 HTML 代码:

<select class = "select2" style = "width:100%" id = "selectme">
     <option value = "oldest"> Person 1</option>
     <option value = "oldest"> Person 2</option>
     <option value = "oldest"> Person 3</option>
</select>

这里是 Select2 的 Jquery:

$("#selectme").select2({
    placeholder: "Assign to:",
    allowClear:true
});

Select2 Documentation 中的示例首先将占位符设置为默认值。但在我的情况下,默认值为 Person 1 而不是占位符。为什么?

【问题讨论】:

    标签: javascript jquery html jquery-select2


    【解决方案1】:

    为了使占位符正常工作,您需要将一个空选项添加到选择列表中,例如:

    <select class="select2" style="width:100%" id="selectme">
        <option value=""></option>
        <option value="oldest">Person 1</option>
        <option value="oldest">Person 2</option>
        <option value="oldest">Person 3</option>
    </select>
    

    FIDDLE DEMO

    【讨论】:

      【解决方案2】:

      如果您没有关闭标签的原始代码,您的 html 代码似乎不正确:

      <select class = "select2" style = "width:100%" id = "selectme"> 
       <option value = "oldest"> Person 1</option>
       <option value = "oldest"> Person 2</option>
       <option value = "oldest"> Person 3</option>
      </select>
      

      【讨论】:

        【解决方案3】:

        html

        <select class="select2" style="width:100%" id="selectme">
            <option value=""></option>
            <option value="oldest">Person 1</option>
            <option value="oldest">Person 2</option>
            <option value="oldest">Person 3</option>
        </select>
        

        jquery

        $(".select2").select2({             
             placeholder: "change your placeholder"               
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-15
          • 2013-10-04
          • 2014-03-13
          • 2018-10-25
          相关资源
          最近更新 更多