我正在尝试使用两个下拉选择菜单制作表单:一个用于选择类别,另一个用于选择子类别。我试图让它成为子类别选项基于他们首先选择的类别,我不知道从哪里开始,谷歌搜索让我空手而归。谢谢!
答案 0 :(得分:1)
搜索级联下拉列表。这需要一些JavaScript或者一些服务器端代码,但是使用这个搜索词,你会发现很多......
从这里开始http://archive.plugins.jquery.com/plugin-tags/drop-down
答案 1 :(得分:1)
这就是你想要的: 请注意,这只是使用html和纯javascript。是的,你也可以使用JQuery。
<html>
<head>
<script language="javascript" type="text/javascript">
function dropdownlist(listindex)
{
document.formname.subcategory.options.length = 0;
switch (listindex)
{
case "category1" :
document.formname.subcategory.options[0]=new Option("subcategory1.1","value1.1");
document.formname.subcategory.options[1]=new Option("subcategory1.2","value1.2");
document.formname.subcategory.options[2]=new Option("subcategory1.3","value1.3");
break;
case "category2" :
document.formname.subcategory.options[0]=new Option("subcategory2.1","value2.1");
document.formname.subcategory.options[1]=new Option("subcategory2.2","value2.2");
document.formname.subcategory.options[2]=new Option("subcategory2.3","value2.3");
break;
case "category3" :
document.formname.subcategory.options[0]=new Option("subcategory3.1","value3.1");
document.formname.subcategory.options[1]=new Option("subcategory3.2","value3.2");
document.formname.subcategory.options[2]=new Option("subcategory3.3","value3.3");
break;
default:
document.formname.subcategory.options[0]=new Option("Select Category")
break;
}
return true;
}
</script>
</head>
<title>Dynamic Drop Down List</title>
<body>
<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">Category :</td>
<td width="59%" align="left" valign="middle"><select name="category" id="category" onchange="dropdownlist(this.options[this.selectedIndex].value);">
<option value="">Select Category</option>
<option value="category1">category 1</option>
<option value="category2">category 2</option>
<option value="category3">category 3</option>
</select></td>
</tr>
<tr>
<td align="right" valign="middle">Sub Category :
</td>
<td align="left" valign="middle"><script type="text/javascript" language="JavaScript">
document.write('<select name="subcategory"><option value="">Select Sub-Category</option></select>')
</script>
<noscript><select name="subcategory" id="subcategory" >
<option value="">Select Sub-Category</option>
</select>
</noscript></td>
</tr>
</table>
</form>
</body>
</html>