0

当用户从主选择框中选择时,我试图通过选择框显示更多选项。我已经放置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>
4

1 回答 1

2

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

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/

于 2013-09-02T21:14:31.383 回答