jQuery Chaining国家/州选择OptGroup

我有以下2个选择列表..我想隐藏State / Prov,直到所选的Country有相应的optgroup标签。 所以..

– 用户选择Alegeria ..没有显示状态选择..

– 用户选择美国或加拿大; “状态选择”仅显示optgroup标签与父名称匹配的值。

合理? 无法找到基于OptGroup的方法,而不必手动为每个子对象分配“类”

 -- Please Select -- Afghanistan Aland Islands United States Algeria Canada Andorra  

-- Please Select -- Alaska Alabama Arkansas Arizona California Alberta British Columbia Manitoba

我想这就是你想要的。

 $(function(){ var state_province = $('#C_State_Prov option, #C_State_Prov optgroup'); state_province.hide(); // hide all state and provinces on page load $('#C_Country').change(function(){ state_province.hide(); // on change of drop down hide all state provinces // find the optgroup with the label = the html of the selected dropdown // select the opt group and all of it's children and show them $("#C_State_Prov optgroup[label='"+$(this).find(':selected').html() + "']") .children() .andSelf() .show(); }); }); 

这可以清理一点,但你明白了。