ASP实现省市联动,其中省市信息是一张表中的,
用一个字段来区分它们是省还是市,通过一个父编号联系省与市的关系,该怎么实现呀?
请大家帮帮忙,谢谢了。
ASP实现省市联动,其中省市信息是一张表中的,
用一个字段来区分它们是省还是市,通过一个父编号联系省与市的关系,该怎么实现呀?
请大家帮帮忙,谢谢了。
数据库名:db1.mdb
表名:citys
字段名:编号(自动编号),省(文本),市(文本)
按照我的表名和字段名建好数据库,
直接新建一页,
把下面代码复制进去,
就ok了
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head>
<body><% set con =server.CreateObject("adodb.connection") provider="provider=microsoft.jet.oledb.4.0;" dbpath="data source="&server.MapPath("database/db1.mdb") con.open provider&dbpath set rs=server.CreateObject("adodb.recordset") sql="select * from citys" rs.open sql,con,3,3 %> <script language="JavaScript"> var onecount=0; subcat = new Array(); <% count = 0 while not rs.eof %> subcat[<%=count%>] = new Array("<%=rs("市")%>","<%= trim(rs("省"))%>"); <% count = count + 1 rs.MoveNext wend rs.close %> onecount=<%=count%>; function changelocation(locationid) { document.form1.city.length = 0; document.form1.city.options[document.form1.city.length] = new Option("-选择市-", ""); var locationid=locationid; for (var i=0;i < onecount; i++) { if (subcat[i][1] == locationid) { document.form1.city.options[document.form1.city.length] = new Option(subcat[i][0], subcat[i][2]); } }
function test(o) { var index = o.selectedIndex; var text = o.options[index].text; document.getElementById("city1").value = text; } } </script> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <%set rs=con.execute("select distinct(省) from citys where 省 in (select 省 from citys group by 省 having(count(省))>1)")' and id not in (select min(id) from diming group by sheng having count(sheng)>1)")%>
<form id="form1" name="form1" method="post" action=""> <select name="province" onchange="changelocation(document.form1.province.options[document.form1.province.selectedIndex].value);" size="1"> <option selected="selected" value="-选择省-">-选择省-</option> <%if rs.eof then%> <option selected="selected" value=""></option> <%else while not rs.eof%> <option value="<%=rs("省")%>"><%=rs("省")%></option> <%rs.MoveNext wend end if %> </select> <select name="city" id="city"> <option selected="selected" value="">-选择市-</option> </select> </form> </body> </html>
一般不用ASP来实现.. 都是用JS代码来实现的.. 如果你非要用ASP来实现的话..那也只有用AJAX来异步执行