2016-12-05 156 views
1

我想根据第一个下拉菜单的选择更改第二个下拉菜单,但目前无法正常工作。我使用bootstrap select的selectpicker和form-group类,但我不确定它是否会导致问题。根据第一个下拉菜单更改第二个下拉菜单

第二个下拉列表在第一个下拉列表被选中之前被禁用,并且只显示与数据组选择匹配的项目。

HTML:

<select id="seasons" class="selectpicker form-group" title="Choose season" onchange="filterSeason();"> 
    <option value='2013-14'>2013-14</option> 
    <option value='2014-15'>2014-15</option> 
    <option value='2015-16'>2015-16</option> 
    <option value='2016-17'>2016-17</option> 
</select> 
<select id="clubs" class="selectpicker form-group" title="Choose club" disabled> 
    <optgroup data-group='2013-14' label="England"> 
     <option data-group='2013-14' value='ENG1'>Team 1</option> 
     <option data-group='2013-14' value='ENG2'>Team 2</option> 
    </optgroup> 
    <optgroup data-group='2014-15' label="Spain"> 
     <option data-group='2014-15' value='SPA1'>Team 3</option> 
    </optgroup> 
    <optgroup data-group='2015-15' label="Germany"> 
     <option data-group='2015-16' value='GER1'>Team 4</option> 
     <option data-group='2015-16' value='GER2'>Team 5</option> 
     <option data-group='2015-16' value='GER3'>Team 6</option> 
    </optgroup> 
</select> 

的jQuery:

function filterSeason() { 
     var seasons = $("#seasons").find('option:selected').text(); 
     $("#option-container").children().appendTo("#clubs"); 
     var selectSeason = $("#clubs").children("[data-group!='"+seasons+"']"); 
     selectSeason.appendTo("#option-container"); 
     $("#clubs").removeAttr("disabled"); 
}; 
+0

所以,你要只与数据组中的项目是selebtable ?请在您的问题中包含'#option-container'值 –

+0

我认为:应该是

回答

0

我觉得这是你需要什么,如果我理解正确的。

使用$("#clubs").children().removeAttr('disabled');,使所有的孩子,然后禁用这是不相关的这种$(selectSeason).attr('disabled','disabled');

请检查此琴

FIDDLE

+0

谢谢! @Mhamhammed Shevil KP。没有班级,但仍然不能与班级合作。猜猜可能会有一些冲突。 – Victor

+0

我真的不明白你的意思是'没有班级,但仍然不能与班级一起工作' –

+0

我的意思是我必须删除“class =”selectpicker form-group“”才能使其工作 – Victor

相关问题