如何先设置占位符,而不是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而不是占位符。为什么呢?
答案 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>
答案 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"
});