简体   繁体   English

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

[英]select box show another based on what selected

I am trying to show more options via selectbox when the user selects from the main select box. 当用户从主选择框中选择时,我试图通过选择框显示更多选项。 I have put display:none for the selectboxes which will show only when the user selects option from the main select box. 我为选择框添加了display:none ,只有当用户从主选择框中选择选项时才会显示。

can someone help me with the either jquery or javascript only for the first option ? 有人可以帮助我使用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 has selectedIndex property which specifies the index of the selected Option, by using jQuery .eq() method, this property and listening to change event you can select the target select element from jQuery index-based collection: 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/ http://jsfiddle.net/7CmYj/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM