簡體   English   中英

如何在Select2中首先設置占位符

[英]How to set placeholder first in 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>

和SelectView的Jquery在這里:

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

Select2文檔中的示例適用於首先將占位符設置為默認值。 但在我的情況下,默認值是Person 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

如果您的原始代碼沒有關閉標記,那么您的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>

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM