JavaScript依赖的下拉列表 [英] JavaScript dependent drop down list
问题描述
如何使用JavaScript创建依赖下拉列表?
How do you make a dependent drop down list with JavaScript?
我需要第一个类别列表来触发其他2个(或更多)类别以显示列表取决于您对第一类的选择。
I need first category list to trigger other 2 (or more) categories to show up with lists that depends on what was your selection on first category.
这可能吗?
这是我的代码:
<code>
function dropdownlist(listindex)
{
document.formname.category2.options.length = 0;
switch (listindex)
{
case "Home Ware" :
document.formname.category2.options[0]=new Option("Select Category2","");
document.formname.category2.options[1]=new Option("Air-Conditioners/Coolers","Air-Conditioners/Coolers");
document.formname.category2.options[2]=new Option("Audio/Video","Audio/Video");
document.formname.category2.options[3]=new Option("Beddings","Beddings");
document.formname.category2.options[4]=new Option("Camera","Camera");
document.formname.category2.options[5]=new Option("Cell Phones","Cell Phones");
break;
case "Education" :
document.formname.category2.options[0]=new Option("Select Category2","");
document.formname.category2.options[1]=new Option("Colleges","Colleges");
document.formname.category2.options[2]=new Option("Institutes","Institutes");
document.formname.category2.options[3]=new Option("Schools","Schools");
document.formname.category2.options[4]=new Option("Tuitions","Tuitions");
document.formname.category2.options[5]=new Option("Universities","Universities");
break;
case "Books" :
document.formname.category2.options[0]=new Option("Select Category2","");
document.formname.category2.options[1]=new Option("College Books","College Books");
document.formname.category2.options[2]=new Option("Engineering","Engineering");
document.formname.category2.options[3]=new Option("Magazines","Magazines");
document.formname.category2.options[4]=new Option("Medicine","Medicine");
document.formname.category2.options[5]=new Option("References","References");
break;
}
return true;
}
</code>
Html:
<form id="formname" name="formname" method="post" action="submitform.asp" >
<table width="50%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td width="41%" align="right" valign="middle">Category1 :</td>
<td width="59%" align="left" valign="middle"><select name="category1" id="category1" onchange="javascript: dropdownlist(this.options[this.selectedIndex].value);">
<option value="">Select Category1</option>
<option value="Home Ware">Home Ware</option>
<option value="Education">Education</option>
<option value="Books">Books</option>
</select></td>
</tr>
<tr>
<td align="right" valign="middle">Category2 :
</td>
<td align="left" valign="middle"><script type="text/javascript" language="JavaScript">
document.write('<select name="category2"><option value="">Select Category2</option></select>')
</script>
<noscript><select name="category2" id="category2" >
<option value="">Select Category2</option>
</select>
</noscript></td>
</tr>
</table>
</form>
我认为不可能通过选择Home ware来创建第三个列表并有不同的选择?
I believe it is not possible to create a third list that would be triggered by choosing "Home ware" and have different selections?
推荐答案
更新03/2018
我审核此解决方案因为OP(thx @stavasknall)告诉我,我的解决方案在Safari中不起作用。
UPDATE 03/2018
I review this solution because an OP (thx @stavasknall) told me that my solution doesn't work in Safari.
在某些浏览器(如Safari)中,< select>
内的< option>
无法隐藏通过CSS,如果你设置(在css或通过Jquery) style =display:none
Safari只是忽略它。
In some browser (like Safari) the <option>
inside a <select>
can't be hided via CSS, if you set (in css or via Jquery) style="display:none"
Safari simply ignores it.
要定位OP目标,需要操作DOM来更改< option>
在e选择中,要做到这一点有很多解决方案我遵循这个:
To target the OP goal is necessary to manipulate the DOM to change the <option>
inside e selection, to do this there are a lot of solution i follow this one:
<form id="formname" name="formname" method="post" action="submitform.asp" >
<table width="50%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td width="41%" align="right" valign="middle">Category1 :</td>
<td width="59%" align="left" valign="middle">
<select name="category1" id="category1">
<option value="">Select Category1</option>
<option value="home_ware">Home Ware</option>
<option value="education">Education</option>
<option value="books">Books</option>
</select>
</td>
</tr>
<tr>
<td align="right" valign="middle">Category2 :</td>
<td align="left" valign="middle">
<select disabled="disabled" class="subcat" id="category2" name="category2">
<option value>Select Category2</option>
<!-- Home Ware -->
<optgroup data-rel="home_ware">
<option value="air-conditioners_coolers">Air-Conditioners/Coolers</option>
<option value="audio-video">Audio/Video</option>
<option value="beddings">Beddings</option>
<option value="camera">Camera</option>
<option value="cell-phones">Cell Phones</option>
</optgroup>
<!-- Education -->
<optgroup data-rel="education">
<option value="Colleges">Colleges</option>
<option value="Institutes">Institutes</option>
<option value="Schools">Schools</option>
<option value="Tuitions">Tuitions</option>
<option value="Universities">Universities</option>
</optgroup>
<!-- Books -->
<optgroup data-rel="books">
<option value="College Books">College Books</option>
<option value="Engineering">Engineering</option>
<option value="Magazines">Magazines</option>
<option value="Medicine">Medicine</option>
<option value="References">References</option>
</optgroup>
</select>
</td>
</tr>
<tr>
<td align="right" valign="middle">Category3 :</td>
<td align="left" valign="middle">
<select disabled="disabled" class="subcat" id="category3" name="category3">
<option value>Select Category3</option>
<!-- Home Ware -->
<optgroup data-rel="home_ware">
<option value="foo1">category3 home ware 1</option>
<option value="foo2">category3 home ware 2</option>
</optgroup>
<!-- Education -->
<optgroup data-rel="education">
<option value="foo3">category3 Education 1</option>
<option value="foo4">category3 Education 2</option>
</optgroup>
<!-- Books -->
<optgroup data-rel="books">
<option value="foo5">category3 Books 1</option>
<option value="foo6">category3 Books 2</option>
</optgroup>
</select>
</td>
</tr>
</table>
var $cat = $("#category1"),
$subcat = $(".subcat");
var optgroups = {};
$subcat.each(function(i,v){
var $e = $(v);
var _id = $e.attr("id");
optgroups[_id] = {};
$e.find("optgroup").each(function(){
var _r = $(this).data("rel");
$(this).find("option").addClass("is-dyn");
optgroups[_id][_r] = $(this).html();
});
});
$subcat.find("optgroup").remove();
var _lastRel;
$cat.on("change",function(){
var _rel = $(this).val();
if(_lastRel === _rel) return true;
_lastRel = _rel;
$subcat.find("option").attr("style","");
$subcat.val("");
$subcat.find(".is-dyn").remove();
if(!_rel) return $subcat.prop("disabled",true);
$subcat.each(function(){
var $el = $(this);
var _id = $el.attr("id");
$el.append(optgroups[_id][_rel]);
});
$subcat.prop("disabled",false);
});
在此脚本中将s html保存在Javascript对象中(基于 < select>
ID
和< optgroup>
数据-rel
)并从DOM中删除它们。使用此解决方案,当< select>
更改脚本时,找到相关选项并将其打印在相对< select> $ c $中c>。
In this the script save the s html inside a Javascript object (based on <select>
ID
and <optgroup>
data-rel
) and remove them from the DOM. With this solution when a <select>
change the script find the related option and print it in the relative <select>
.
与其他解决方案一样,你必须包含jQuery,你必须将Javascript包装在 DOMready listener $ c中$ c>或在
< body>
的末尾。
Like in other solution also in this one you must include jQuery and you have to wrap the Javascript inside DOMready listener
or at the end of the <body>
.
http://jsfiddle.net/v917ycp6/595
我改变你的 HTML
, JS
, CSS
以防止过度使用Javascript。
I change your HTML
, JS
, CSS
to prevent the excessive use of Javascript.
- 首先:考虑使用
jQuery
(一个简单的JS库,帮助你创建复杂的东西) - 不创建/ remvoe DOM元素每次需要更改HTML时 - 改为隐藏这个元素并显示你所有的内容。
- First: Consider to use
jQuery
(a simple JS library that help you to create complex thing) - Not create/remvoe DOM element every time you need to change your HTML - instead HIDE this element and show all you nedd.
HTML
HTML
<form id="formname" name="formname" method="post" action="submitform.asp" >
<table width="50%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td width="41%" align="right" valign="middle">Category1 :</td>
<td width="59%" align="left" valign="middle">
<select name="category1" id="category1">
<option value="">Select Category1</option>
<option value="home_ware">Home Ware</option>
<option value="education">Education</option>
<option value="books">Books</option>
</select>
</td>
</tr>
<tr>
<td align="right" valign="middle">Category2 :</td>
<td align="left" valign="middle">
<select disabled="disabled" id="category2" name="category2">
<option class="label" value>Select Category2</option>
<!-- Home Ware -->
<option rel="home_ware" value="air-conditioners_coolers">Air-Conditioners/Coolers</option>
<option rel="home_ware" value="audio-video">Audio/Video</option>
<option rel="home_ware" value="beddings">Beddings</option>
<option rel="home_ware" value="camera">Camera</option>
<option rel="home_ware" value="cell-phones">Cell Phones</option>
<!-- Education -->
<option rel="education" value="Colleges">Colleges</option>
<option rel="education" value="Institutes">Institutes</option>
<option rel="education" value="Schools">Schools</option>
<option rel="education" value="Tuitions">Tuitions</option>
<option rel="education" value="Universities">Universities</option>
<!-- Books -->
<option rel="books" value="College Books">College Books</option>
<option rel="books" value="Engineering">Engineering</option>
<option rel="books" value="Magazines">Magazines</option>
<option rel="books" value="Medicine">Medicine</option>
<option rel="books" value="References">References</option>
</select>
</td>
</tr>
</table>
</form>
看看:我使用 rel
属性链接类别和子类别(小写,没有空格或特殊字符):
take a look: I use rel
attribute to link category and sub category (to lowercase and without space or special Char):
<option rel="home_ware" value="">Select Category1</option>
CSS
CSS
#category2 option{
display:none;
}
#category2 option.label{
display:block;
}
此CSS隐藏仅显示主要内容的子类别选项(非标签)类别被选中。
this CSS hide the subcategory options (not label) that show only if the main category is seleceted.
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script
<script>
$(function(){
//fisrt I store in 2 var the reference of two <select>
var $cat = $("#category1"),
$subcat = $("#category2");
//this is the same thing if you write in your HTML onChange="" in first <select>
$cat.on("change",function(){
//store the value of first select every time it change
var _rel = $(this).val();
//clean the second select (value and option active) to prevent bad link (cat1 with subcat of cat2)
$subcat.find("option").attr("style","");
$subcat.val("");
//if no option is selected i disable the second select
if(!_rel) return $subcat.prop("disabled",true);
//if a option si selected i show the option linked by rel attr
$subcat.find("[rel="+_rel+"]").show();
$subcat.prop("disabled",false);
});
});
</script>
我使用jQuery因为简单,干净,而且你编写的代码更少。
查看jQuery的文档: http://api.jquery.com/ 。如果你从不使用它,你应该。
I use jQuery because is simple, clean, and you write less code. Take a look to jQuery's Documentation: http://api.jquery.com/. If you never use it, you should.
需要这个重新出现
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
工作示例:
http://jsfiddle.net/v917ycp6/5/
如果您需要在选中<$的一个选项后启用多个< select>
c $ c> category1 您只需更改变量 $ subcat
的选择器即可选择多个< select>
。
If you need to enable multiple <select>
after you check one option of category1
you simply change the selector of the variable $subcat
to select more than one <select>
.
添加类 .subcat
到所有< select>
您需要在选择 category1
时启用:
add class .subcat
to all <select>
you need to enable when select category1
:
<select disabled="disabled" id="category2" name="category2">
变为:
<select disabled="disabled" id="category2" class="subcat" name="category2">
现在,添加新的< select>
到你的代码:
Now, add the new <select>
to your code:
<select disabled="disabled" class="subcat" id="category3" name="category3">
<option value>Select Category3</option>
<!-- Home Ware -->
<option rel="home_ware" value="foo1">category3 home ware 1</option>
<option rel="home_ware" value="foo2">category3 home ware 2</option>
<!-- Education -->
<option rel="Education" value="foo3">category3 Education 1</option>
<option rel="Education" value="foo4">category3 Education 2</option>
<!-- Books -->
<option rel="Books" value="foo5">category3 Books 1</option>
<option rel="Books" value="foo6">category3 Books 2</option>
注意: < option>
已经 rel
$ subcat = $(#category2);
变为 $ subcat = $(。subcat);
.subcat option{
display:none;
}
.subcat option.label{
display:block;
}
#category2
成为 .subcat
。
此实现概括了每个 select $ c $的行为c>具有类
subcat
。
This implementation generalizes the behavior for each select
that has class subcat
.
http://jsfiddle.net/v917ycp6/9/
我不是英文,现在我没有时间纠正我的语法错误,抱歉。 :(我的坏。
这篇关于JavaScript依赖的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!