从网上下了一个省市县三级联动的代码。
本帖最后由 cross2010_0 于 2013-12-19 13:06:02 编辑
<%
Option Explicit
'数据库:
'test
'表1 S_Province 省 表
' 字段
' ProvinceID (主键)
' ProvinceName 名字
'表2 S_City 市 表
' 字段
' CityID (主键)
' CityName 名字
' ProvinceID
'表3 S_District 区县 表
' 字段
' DistrictID (主键)
' DistrictName
' CityID
%>
<!--#include file="conn.asp"-->
<html>
<head>
<title>asp三级联动菜单(含数据库)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%
dim conn
dim rs
dim sql
dim count
dim rs1
dim sql1
dim rs2
dim sql2
dim count2
dim connstr
set rs=server.createobject("adodb.recordset")
sql = "select * from S_City order by CityID"
rs.open sql,conn,1,1
%>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<%
count = 0
do while not rs.eof
%>
subcat[<%=count%>] = new Array("<%=trim(rs("CityName"))%>","<%= trim(rs("Provinceid"))%>","<%= trim(rs("CityID"))%>");
<%
count = count + 1
rs.movenext
loop
rs.close
set rs=nothing
%>
onecount=<%=count%>;
function chsel1(provinceid)
{
document.myform.class2.length = 0;
document.myform.class3.length = 0;
document.myform.class3.options[0] = new Option('==请选择省==','');
var provinceid=provinceid;
var i;
document.myform.class2.options[0] = new Option('==请选择市==','');
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == provinceid)
{
document.myform.class2.options[document.myform.class2.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}
</script>
<%
set rs2=server.createobject("adodb.recordset")
sql2 = "select * from S_District order by districtid asc"
rs2.open sql2,conn,1,1
%>
<script language = "JavaScript">
var onecount2;
onecount2=0;
subcat2 = new Array();
<%
count2 = 0
do while not rs2.eof
%>
subcat2[<%=count2%>] = new Array("<%= trim(rs2("Districtname"))%>","<%= trim(rs2("Cityid"))%>","<%= trim(rs2("Districtid"))%>");
<%
count2 = count2 + 1
rs2.movenext
loop
rs2.close
set rs2=nothing
%>
onecount2=<%=count2%>;
function chsel2(districtid)
{
document.myform.class3.length = 0;
var districtid=districtid;
var j;
document.myform.class3.options[0] = new Option('==请选择县==','');
for (j=0;j < onecount2; j++)
{
if (subcat2[j][1] == districtid)
{
document.myform.class3.options[document.myform.class3.length] = new Option(subcat2[j][0], subcat2[j][2]);
}
}
}
</script>
</head>
<body>
<form action="add.asp" method="get" name="myform">
<p><br>
<select name="class1" onChange="chsel1(document.myform.class1.options[document.myform.class1.selectedIndex].value)">
<option selected value="">==请选择省==</option>
<%
set rs1=server.createobject("adodb.recordset")
sql1 = "select * from S_Province order by Provinceid asc"
rs1.open sql1,conn,1,1
do while not rs1.eof
%>
<option value="<%=rs1("provinceid")%>"><%=rs1("provincename")%></option>
<%
rs1.movenext
loop
rs1.close
set rs1 = nothing
conn.Close
set conn = nothing
%>
</select>
</p>
<p>
<select name="class2" onChange="chsel2(document.myform.class2.options[document.myform.class2.selectedIndex].value)">
<option selected value="">==请选择市==</option>
</select>
</p>
<p>
<select name="class3">
<option selected value="">==请选择县==</option>
</select>
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form>
</body>
</html>