2016-01-29 27 views
0

當您使用select2'多選框'選項時,它提供了輸入文本的選項。我想刪除此功能,以便用戶只能點擊/觸摸可用選項,並且文本光標不會顯示。如何在無法輸入文字的情況下在Select2中使用'多個選擇框'選項?

你可以看到他們的榜樣頁上的「多選擇框」選項:https://select2.github.io/examples.html

我的代碼。 (我使用Laravel 5刀模板)

<div class="row searchBoxes show"> 
    {!! Form::open(['method' => 'GET', 'id' => 'searchForm', 'name' => 'searchForm', 'route' => 'articles_path']) !!} 
     {!! Form::select('categoryList[]', $categories, null, ['class' => 'categorySelector', 'id' => 'categoryList', 'multiple']) !!} 
     {!! Form::select('dayList[]', $days, null, ['class' => 'distanceSelector', 'id' => 'dayList', 'multiple']) !!} 
    {!! Form::submit('MAKE ME HAPPY', array('id' => 'submitButton', 'class' => 'searchSubmit')) !!} 
    {!! Form::close() !!} 
</div> 

    <script type="text/javascript"> 
    $('#categoryList').select2({ 
      placeholder: "CATEGORY", 
      minimumResultsForSearch: -1 
      }); 
     $('#dayList').select2({ 
     placeholder: "DAY", 
     minimumResultsForSearch: -1 
     }); 
    </script> 

編輯: 我發現了大約'Hiding the search box'的信息。我試圖將minimumResultsForSearch切換到'無限',但它仍然無法工作。這是我的應用程序:www.gethappy.co.nz

回答

0

我最終只是編輯select2.js文件的代碼。

我改變了這一部分(大約1810行)

Search.prototype.render = function (decorated) { 
    var $search = $(
     '<li class="select2-search select2-search--inline">' + 
     '<input class="select2-search__field" type="search" tabindex="-1"' + 
     ' autocomplete="off" autocorrect="off" autocapitalize="off"' + 
     ' spellcheck="false" role="textbox" aria-autocomplete="list" />' + 
     '</li>' 
    ); 

要這樣:

Search.prototype.render = function (decorated) { 
    var $search = $(
     '<li>' + 
     '<input tabindex="-1"' + 
     ' autocomplete="off" autocorrect="off" autocapitalize="off"' + 
     ' spellcheck="false" role="textbox" aria-autocomplete="list" style="display: none" />' + 
     '<p class="placeHolderText"></p>' + 
     '</li>' 
    ); 

我不得不在裏面添加一個新的佔位符作爲擺脫了選擇2佔位符。然後我用jquery添加了佔位符文本。

還有其他答案HERE但他們沒有爲我工作。

1

使用屬性minimumResultsForSearch並將其設置爲-1

$('#categoryList').select2({ 
    placeholder: "CATEGORY", 
    minimumResultsForSearch: -1 
}); 

Example

的問題被列入here

+0

謝謝,這不適合我?我在我的選擇下拉列表中有4個選項。嘗試將minimumResultsForSearch更改爲10。沒有工作。有任何想法嗎? –

+0

你能發佈html嗎?這將有助於找出問題。 – DinoMyte

+0

增加了更多的代碼。目前它工作正常,但無法擺脫文本輸入。 –

相關問題