2016-11-09 83 views
2

正如你可以看到我有与选择2(多)选择功能选择框,有些选项像如果选择特定的值与选择2

<option value="All"> All </option> 
<option value="[email protected]"> User 1</option> 
<option value="[email protected]"> User 2 </option> 
<option value="[email protected]"> User 3 </option> 

的是,尽量不要让选择其他选项,如果我从选择框中选择选项All,然后选择框剂量不允许我从给定选项中选择其他选项,除非我删除它。

我想限制用户选择要么选择All或选择从选项的多个用户。

我准备使用jQuery或JavaScript来实现这一目标。

+0

尝试HTML'disabled'属性。 – Xorifelse

+0

战胜饥饿国家联盟,我不想禁用,如果他选择全部,然后再后,他尽量选择用户则应全部被删除,选择的选项应与允许多个功能 –

+0

你认为你其实可以只用HTML做这个选择?我再说一次,在选择'all'时禁用所有其他选项,当重新选择删除禁用的属性哦,是的,你需要JavaScript。 – Xorifelse

回答

2

Working fiddle

选项All呈现时,您可以设置进行选择,以全价值:

$("#my-select").select2(); 

$("#my-select").on('change', function(){ 
    var selected = $(this).val(); 

    if(selected != null) 
    { 
     if(selected.indexOf('All')>=0){ 
      $(this).val('All').select2(); 
     } 
    } 
}) 

希望这有助于。

$("#my-select").select2(); 
 

 
$("#my-select").on('change', function(){ 
 
    var selected = $(this).val(); 
 

 
    if(selected != null) 
 
    { 
 
    if(selected.indexOf('All')>=0){ 
 
     $(this).val('All').select2(); 
 
    } 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.min.js"></script> 
 

 
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.css" rel="stylesheet"/> 
 

 
<select multiple id="my-select" style="width:300px"> 
 
    <option value="All"> All </option> 
 
    <option value="[email protected]"> User 1</option> 
 
    <option value="[email protected]"> User 2 </option> 
 
    <option value="[email protected]"> User 3 </option> 
 
</select>