2016-04-08 27 views
1

我使用Select2(v3.5.2)並嘗試允許用戶從多個下拉列表中選擇一個或多個值。Select2允許重新選擇相同的值

想象一下這個簡單的僞HTML標記:

<select class='select2' multiple> 
<option value="A">A</option> 
<option value="B">B</option> 
<option value="C">C</option> 
</select> 

和jQuery:

$(document).ready(function() { 
    $('.select2').select2(); 
}); 

默認功能允許選擇的選項(A,B,C)只有一次,在我的情況,我需要用戶能夠不止一次地選擇相同的選項以使得以下成爲可能:

A,A,A,B,C 
A,B,B,B,C,C 
A,A,A,B,B,C,C,C 
etc... 

從我的搜索,它似乎不受版本3.5.2支持,但一些職位提到它在版本4.0.x支持。

但是,我無法找到有關v4的任何文檔,並且真的寧願不升級到4。

有誰知道如何使它與版本3(或4)?

回答

2

正如您所提到的,似乎選擇多個相似值的功能在版本4.0中已得到修復,但我無法在文檔中找到對此的參考。所以我不得不使用select2 3.2v框架編寫功能。 如果仔細觀察呈現的DOM,您會意識到select2基本上會添加類select2-disabled並刪除類select2-result-selectable,一旦您從下拉列表中選擇一個項目,那麼交易將返回select2-result-selectable

// initiate select2 
$('.select2').select2(); 
// delegate a click event on the input box 
$('.select2-input').on('click',function() 
{ 
    // remove select2-disabled class from all li under the dropdown 
    $('.select2-drop .select2-results li').removeClass('select2-disabled'); 
    // add select2-result-selectable class to all li which are missing the respective class 
    $('.select2-drop .select2-results li').each(function() 
    { 
    if(!$(this).hasClass('select2-result-selectable')) 
     $(this).addClass('select2-result-selectable'); 
    }); 
}); 

    // had to include the following code as a hack since the click event required double click on 'select2-input' to invoke the event 
$('.select2-container-multi').on('mouseover',function() 
{ 
    $('.select2-input').click(); 
}); 

工作例如:https://jsfiddle.net/DinoMyte/qcgvucnz/1/

+0

如何與選擇2 4.0版本實現這一目標的任何文檔? – Gowrav