省市联动菜单的源代码实现(access数据库版)

作者:网络 来源:佚名 更新时间:2008-02-05 14:34:28 点击:
在网上经常看到关于联动菜单的贴子,特将自己的代码贴出来,供大学参与.
一.数据库:
表1:region(id,province)
表2:city(id,city,province)
二.代码实现:
<!--#include file="conn.asp"-->
<%
function dbcombox()
dim rs,sql,msg
sql = "select * from region"
set rs = conn.execute(sql)
while not rs.eof
msg = msg & "<option value=""" & rs("province") & """>" & rs("province") & "</option>"
rs.movenext
wend
rs.close
set rs = nothing
dbcombox = msg
end function
%>
<html>

<head>
<meta http-equiv="content-language" content="zh-cn">
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>会员登录</title>
</head>

<body>
<script language ="javascript" >
citys = new array();
<%
dim rs,sql,i
sql = "select * from city"
set rs = conn.execute(sql)
i = 0
while not rs.eof
%>
citys[<%=i%>] =new array("<%=rs("province")%>","<%=rs("city")%>");
<%
i = i + 1
rs.movenext
wend
rs.close
set rs = nothing
%>

function changeselect(selvalue){
var selvalue = selvalue;
var i;
document.form1.city.length = 0 ;
document.form1.city.options[document.form1.city.length] = new option("请选择","");
for (i = 0 ;i <citys.length;i++){
if(citys[i][0]==selvalue){
document.form1.city.options[document.form1.city .length] = new option(citys[i][1],citys[i][1]);
}
}
}

document.form1.city.options[document.form1.city.length] = new option("请选择","");

</script>

<div align="center">
<form method="post" action="forum.asp" name="form1">
<table border="0" cellpadding="0" style="border-collapse: collapse" width="100%" id="table1" height="59">
<tr>
<td width="67" height="30" bgcolor="#d4d0c8"><b><font size="2">地区</font></b></td>
<td height="30" bgcolor="#d4d0c8"><select size="1" name="region" onchange ="changeselect(document.form1.region.options[document.form1.region.selectedindex].value)">
<%=dbcombox()%></select></td>
</tr>
<tr>
<td width="67" height="30"><b><font size="2">城市</font></b></td>
<td height="30"><select size="1" name="city"></select></td>
</tr>
</table>
</form>
</div>

</body>

</html>