选择框根据所选内容显示另一个

select box show another based on what selected

本文关键字:显示 另一个 选择      更新时间:2023-09-26

当用户从主选择框中进行选择时,我正试图通过选择框显示更多选项。我为选择框设置了display:none,只有当用户从主选择框中选择选项时才会显示。

有人能帮我处理jquery或javascript中的第一个选项吗?

 .sub{display:none;}
 <select> <!--This is main selectbox.-->
 <option value="">Select</option>
 <option>ONE</option>
 <option>two</option>
 <option>three</option>
 <option>four</option>
 <option>five</option>
 </select>

 <select class="sub"><!--another selectbox for option one.-->
 <option>test1</option>
 <option>test1</option>
 </select>
 <select class="sub"><!--another selectbox for option two.-->
 <option>test2</option>
 <option>test2</option>
 </select>

 <select class="sub"><!--another selectbox for option three.-->
 <option>test3</option>
 <option>test3</option>
 </select>

 <select class="sub"><!--another selectbox for option four.-->
 <option>test4</option>
 <option>test4</option>
 </select>

 <select class="sub"><!--another selectbox for option five.-->
 <option>test5</option>
 <option>test5</option>
 </select>

DOM Select Element具有selectedIndex属性,该属性指定所选Option的索引,通过使用jQuery .eq()方法,该属性和侦听change事件,可以从基于jQuery索引的集合中选择目标Select元素:

var $sub = $('select.sub');
$('select').first().change(function() {    
    $sub.hide();
    // If the first option is not selected
    if (this.selectedIndex > 0)
       $sub.eq(this.selectedIndex - 1).show();
});

http://jsfiddle.net/7CmYj/