2016-04-08 28 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