<html>
<head>
<title>三级联动下拉表单试验</title>
<!--=======打开数据库======-->
<%
Set Conn = Server.CreateObject("ADODB.Connection")
StrCnn = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("db1.mdb") & ";"
Conn.Open StrCnn
%>
</head>
<body>
<form name="myform" method="post">
<%
set rs=server.CreateObject("adodb.recordset")
sq="select * from 一级表"
rs.open sq,conn,1,1
%>
<select name="class1" onChange="changeclass2();changeclass3()">
<option value="" selected>选择一级目录</option>
<%
while not rs.eof
%>
<option value="<%=rs("一级类")%>"><%=rs("一级类")
%>
</option>
<%
rs.movenext
wend
rs.close
%>
</select>
<select name="class2" onChange="changeclass3()">
<option value="" selected>选择二级目录</option>
</select>
<select name="class3">
<option value="" selected>选择三级目录</option>
</select>
<%
sql="select * from 二级表"
rs.open sql,conn,1,1
num=rs.recordcount
str=""
for i=1 to rs.recordcount
str=str&rs("一级类")&"-"&rs("二级类")&","
if rs.eof then exit for
rs.movenext
next
rs.close
%>
<%
sql="select * from 三级表"
rs.open sql,conn,1,1
num2=rs.recordcount
str2=""
for i=1 to rs.recordcount
str2=str2&rs("二级类")&"-"&rs("三级类")&","
if rs.eof then exit for
rs.movenext
next
rs.close
%>
<!--下面是实现动态改变下一级菜单的脚本代码-->
<script LANGUAGE="javascript">
arr="<%=str%>".split(",");
a=arr.length
ar=new Array()
for (i=0;i<a;i++){
ar[i]=arr[i].split("-");
}
onecount=ar.length;
arr2="<%=str2%>".split(",");
a2=arr2.length
ar2=new Array()
for (i=0;i<a2;i++){
ar2[i]=arr2[i].split("-");
}
onecount2=ar2.length;
function changeclass2() {
document.myform.class2.length=0
lid=myform.class1.value;
for (i=0;i<onecount;i++) {
if (ar[i][0] == lid) {
document.myform.class2.options.add(new Option(ar[i][1], ar[i][1]));
}
}
}
function changeclass3() {
document.myform.class3.length=0
lid2=myform.class2.value;
for (i=0;i<onecount2;i++) {
if (ar2[i][0] == lid2) {
document.myform.class3.options.add(new Option(ar2[i][1], ar2[i][1]));
}
}
}
</script>
参考有关资料,编了这个三级联动下拉表单的源代码,二级和四级联动下拉表单可以在此基础上简单改一下就行了。这个代码由于使用“-”作为分隔符,所以数据库中不能有“-”,如果一定要有“-”,可以把源代码改一下就行了。
</body>
</html>