具体做法如下:
1.设计数据库
id—自动编号 ProvinceName—省名 ProvinceNo—省名编号 ProvinceOrder—省排序编号
省表
id | ProvinceName | ProvinceNo | ProvinceOrder |
1 | 北京市 | 1 | 1 |
2 | 安徽省 | 2 | 2 |
3 | 山东省 | 3 | 3 |
4 | 内蒙古自治区 | 4 | 4 |
市表
id | CityName | CityNo | CityOrder | ProvinceID |
1 | 北京市 | 1 | 1 | 1 |
2 | 合肥市 | 2 | 2 | 2 |
3 | 芜湖市 | 3 | 3 | 2 |
4 | 安庆市 | 4 | 4 | 2 |
5 | 济南市 | 5 | 5 | 3 |
6 | 呼和浩特市 | 6 | 6 | 4 |
以下市名略。
用Javascript和Vbscript将Province和City表中的数据读出来并使Province_select和City_select联系起来,这一步是整个程序的关键所在。
需要包含一个链接数据库的网页dbpass.asp。内容如下:
<%
Const constDSN = "NpoMy"
Const constUID = "sa"
Const constPWD = "sa"
%>
需要在开始
联动网页代码:
<!--#include file="includes/dbpass.asp"-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>二级联动菜单</title>
<%
'连接数据库
dim db,conn,rs_Province,rs_City
set conn = server.createobject("adodb.connection")
conn.open constDSN, constUID, constPWD
%>
<script language=JavaScript>
<%
dim sql,i,j
'//读出 Province 表//
set rs_Province= server.createobject("adodb.recordset")
sql="select * from changeless.dbo.Province order by ProvinceOrder"
rs_Province.open sql,conn,1,1,&h0001
%>
var selects=[];
selects['yyy']=new Array(new Option('请选择城市……','yyy'));
<%
for i=1 to rs_Province.recordcount
%>
selects['<%=rs_Province("ProvinceNo")%>']=new Array(
<%
'//读出 City 表//
set rs_City= server.createobject("adodb.recordset")
sql="select * from changeless.dbo.City where ProvinceID="&rs_Province("ProvinceNo")&" order by CityOrder"
rs_City.open sql,conn,1,1,&h0001
if rs_City.recordcount>0 then
for j=1 to rs_City.recordcount
if j=rs_City.recordcount then
%>new Option('<%=trim(rs_City("CityName"))%>','<%=trim(rs_City("CityNo"))%>'));
<%else%>
new Option('<%=trim(rs_City("Cityname"))%>','<%=trim(rs_City("CityNo"))%>'),
<%
end if
rs_City.movenext
next
else
%>
new Option('','0'));
<%
end if
rs_City.close
set rs_City=nothing
rs_Province.movenext
next
rs_Province.close
set rs_Province=nothing
%>
<!--//JavaScript控制联动///-->
function chsel(){
with (document.form1){
if(Province_select.value!="yyy") {
City_select.options.length=0;
City_select.add(selects['yyy'][0]);
for(var i=0;i<selects[Province_select.value].length;i++){
City_select.add(selects[Province_select.value][i]);
}
}
}
}
</script>
</head>
<body>
<FORM name="form1">
<!--//Province_select下拉列表//-->
<select name="Province_select" onChange=chsel()>
<option value="xxx" selected>请选择省份……</option>
<%
dim tmpid '定义一个临时变量用来记住省id
tmpid=0
set rs_Province=server.CreateObject("ADODB.recordset")
sql="select * from changeless.dbo.Province order by ProvinceOrder"
rs_Province.open sql,conn,1,1
while not rs_Province.eof
tmpid=rs_Province("id")
%>
<option value="<%=rs_Province("ProvinceNo")%>" ><%=trim(rs_Province("ProvinceName"))%></option>
<%
'response.Write form1.province_select.value
rs_Province.movenext
wend
rs_Province.close
set rs_Province=nothing
%>
</select>
<select name="City_select">
<option value="yyy" selected="selected">-城市-</option>
</select>
</FORM>
</body>
</html>
标签:Province,City,ASP,set,rs,菜单,联动,sql,select From: https://blog.51cto.com/haibo0668/5854374