[英]How to I modify the code to apply it for three drop-downs? (Jquery)
我有一个jQuery代码,该代码根据先前选择的下拉列表值而变化。 当我有两个下拉菜单时,我会使用它,并且它可以完美地工作。
现在的问题是我正在使用3个下拉菜单,并且无法根据3个下拉菜单修改代码(原因是我是jquery的新手)。
这是代码:
jQuery:
jQuery(function(){
var $cat = $('select[name=coursename]'),
$items = $('select[name=semno]');
$cat.change(function(){
var $this = $(this).find(':selected'),
rel = $this.attr('rel'),
$set = $items.find('option.' + rel);
if ($set.size() < 0) {
$items.hide();
return;
}
$items.show().find('option').hide();
$set.show().first().prop('selected', true);});});
我用了两个下拉菜单即coursename和semno,与此代码和它完美的罚款。
现在,我想添加另一个下拉菜单, subnm ,它在semno之后。 因此,我真正想要的是,当一个人在课程名称中进行特定选择时,相关项目应出现在semno中,而在这些相关项目中,当选择一个值时,这些项目将相应地列在subnm上 。
我在option元素中使用了rel和class。
HTML代码
Course:
<select name="coursename" id="coursename">
<option value="xyz" rel="xyz">XYZ</option>
<option value="abc" rel="abc">ABC</option>
</select>
Semester:
<select name="semno" id="sem">
<option value="one" class="xyz">I</option>
<option value="two" class="xyz">II</option>
<option value="three" class="abc">III</option>
</select>
Subject:
<select name="subnm" id="subnm">
<option value="p">p</option>
<option value="q">q</option>
<option value="r">r</option>
</select>
我想我将需要在semno下拉列表上使用rel选项,然后根据semno rel在subnm下拉列表上进行分类 。
如果我不能百分百理解,请原谅我。 我是这个网站的新手,我真的需要帮助。
先感谢您!
希望这就是你想要的。 我为第二个select
菜单使用了相同的更改事件功能。
$items.change(function(){
var $this = $(this).find(':selected'),
rel = $this.attr('rel'),
$set = $third.find('option.' + rel);
if ($set.size() < 0) {
$third.hide();
return;
}
$third.show().find('option').hide();
$set.show().first().prop('selected', true);
});
我也已经在first select
change
事件处理程序中触发了第二select
的change
事件。
$items.trigger("change");
请参考这个小提琴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.