2014-01-15 127 views
0

我正在使用BootStrap选择多选目的。如果我有10个选项,我选择3个或更多的选项,现在要选择第四个选项,然后其他选择的选项取消选择随机。我使用的自举选择以下功能:使用Bootstrap选择多个选择不能正常工作

$('#deptID').selectpicker('refresh'); 
$('#'+entry).prop('disabled',false); 

BootStrap-Select Reference

选择

<select id="years" class="select" data-live-search="true" class="selectpicker col-md-12" onchange="change_in_year()" multiple> 
<option id="2011" value="2011">2011</option> 
<option id="2012" value="2012">2012</option> 
<option id="2013" value="2013">2013</option> 
<option id="2014" value="2014">2014</option> 
<option id="2015" value="2015">2015</option> 
<option id="2016" value="2016">2016</option> 
<option id="2017" value="2017">2017</option> 
<option id="2018" value="2018">2018</option> 
</select> 

change_in_year()

change_in_year() 
{ 
var yearSelect = new Array(); 
var i = 0; 


$("#years option:selected").each(function(){ 
     yearSelect[i] = $(this).val(); 
     i++; 
}); 

var yearS = new Array(); 
var i = 0; 

$("#years option").each(function(){ 
     yearS[i] = $(this).val(); 
     i++; 
}); 

if(yearSelect[0] == 0) 
{ 


     yearS.forEach(function(entry) { 
      if(entry != "0") 
      { 
      $('#'+entry).prop('disabled',true); 
      $('#'+entry).prop('selected',false); 
      } 
     }); 
     $('#years').selectpicker('refresh'); 
} 
if((yearSelect[0] != 0)) 
{ 

     yearS.forEach(function(entry) { 
      $('#'+entry).prop('disabled',false); 
     }); 
     $('#years').selectpicker('refresh'); 
} 
} 

这是实际工作的代码和我一样,可以正常使用在jsFiddle。但在其他选定的选项

jsFiddle Link

+1

你能否提供一些更多的代码或创建一个jsfiddle。 –

+0

添加了使用引导选择的代码 – Manu

+1

似乎工作正常(http://bootply.com/106018),但是你的代码初始化'#年'的选择在哪里? – ZimSystem

回答

0

上述工作晴在我的jsfiddle系统选择的选项取消选择的原因。我更改了jquery.js和bootstrap.js。现在它的工作在我的系统中也很好..