0

我正在尝试使用两个下拉选择菜单制作表单:一个选择类别,另一个选择子类别。我正在努力做到这一点,因此子类别选项基于他们首先选择的类别,我不知道从哪里开始,谷歌搜索让我空手而归。谢谢!

4

2 回答 2

1

搜索级联下拉菜单。这将需要一些 JavaScript 或者一些服务器端代码,但是使用这个搜索词你会发现很多......

并从这里开始http://archive.plugins.jquery.com/plugin-tags/drop-down

于 2012-06-14T17:13:03.073 回答
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>
于 2012-06-14T17:20:18.613 回答