如何在Select2

时间:2015-09-15 13:40:10

标签: javascript jquery html jquery-select2

如何先设置占位符,而不是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>

和Jquery for Select2在这里:

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

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

3 个答案:

答案 0 :(得分:7)

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

<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

答案 1 :(得分:0)

如果您的原始代码没有关闭代码,那么您的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>

答案 2 :(得分:0)

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"               
});