2015-09-20 148 views
0

我想顯示使用佔位符的多選框(沒有不尋常的)。我已經在列表中添加了一個空白項目,可以按預期方式顯示佔位符。Select2 4.0多選框和佔位符顯示空白項目與選擇

<select id="county_id" name="county_id" multiple="" tabindex="-1"  class="select2-hidden-accessible" aria-hidden="true"> 
<option value="" selected="selected"></option> 
<option value="1">Adams</option> 
<option value="2">Ashland</option> 
<option value="3">Barron</option> 

但是,當我在列表中選擇一個項目,一個空白框(第一個空白選項)顯示爲第一選擇,我選擇的項目之前。注意下面的第一個列表項:

<span class="select2-selection select2-selection--multiple" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" tabindex="0"> 
<ul class="select2-selection__rendered"> 
<span class="select2-selection__clear">×</span> 
<li class="select2-selection__choice" title=""><span class="select2-selection__choice__remove" role="presentation">×</span></li> 
<li class="select2-selection__choice" title="Kenosha"><span class="select2-selection__choice__remove" role="presentation">×</span>Kenosha</li> 

如果我不預置與空白選項列表,然後佔位符不工作,第一個列表項被默認選中。

select2網站上最接近的例子使用了選項組(我的列表沒有),並且似乎沒有空白選項。我不記得用v3.5x來解決這個問題。除了佔位符,我沒有設置任何選項。我可能做錯了什麼?我怎樣才能擺脫這個額外的空白選擇?

回答

0

你是否在使用Formtastic?我有這個問題使用Formtastic。如果沒有,也許這可以幫助你引導你正確的方向。我不得不通過:include_blank => false進入我的表格刪除空白元素。 docs:prompt – by default, all select inputs will have a blank option at the top of the list. You can add a prompt with the :prompt option, or disable the blank option with :include_blank => false.也許這是類似的東西?