今天在公司裡項目裡要用到二級聯動,還是ASP的,ASP.NET的話就好搞定。好長時間沒有動過ASP了。
在網上找了些資料改了改。實現了這個功能,代碼如下:
ASP二級聯動
省表:sheng:sid,stitle.學校表:cid,ctitle,sid
<!--#include file="../sub/conn.asp"-->
<html><head><title></title>
<script type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>
<body>
<form method="post" name="myform" id="myform" >
<select name="first" onChange="MM_jumpMenu('parent',this,0)">
<%
sqlt2 = "select * from sheng"
set rst2=server.createobject("adodb.recordset")
rst2.open sqlt2,conn,0,1
if rst2.eof or rst2.bof then
response.write "<option>-請選擇-</option>" '如果沒有記錄,就顯示-----
else
while not(rst2.eof or rst2.bof) '有記錄,就將父名稱列出來,形成下拉。
%>
<option value="?sec=<%=rst2("sid")%>&class_1=<%=rst2("stitle")%>" <%if cstr(rst2("sid")) = request.querystring("sec") then %>selected<%end if%>><%=rst2("stitle")%></option>
<%rst2.movenext
wend
rst2.movefirst '將遊標移到第一條,以備下面之用。
end if
%>
</select>
<input name="class_1" type="hidden" id="class_1" value="<%=request("class_1")%>">
<select name="second">
<%
if rst2.eof or rst2.bof then
response.write ("<option>-請選擇-</option>")
else
if request.querystring("sec") = "" then '擷取跳轉之後的sec值
temp=rst2("sid")' 如果為空白,就把temp的值設為第一條記錄的值
else
temp = request.querystring("sec") '否則就為收到的值
end if
subsql = "select * from school where sid="&temp&""
set subrs=server.createobject("adodb.recordset")
subrs.open subsql,conn,0,1
if subrs.eof or subrs.bof then
response.write ("<option>-請選擇-</option>")
'如果沒有記錄,則在這個列表中顯示"-----"
else
while not(subrs.eof or subrs.bof)'否則就用迴圈列出一切合格記錄。
response.write ("<option value=" & subrs("ctitle") & ">" & subrs("ctitle") & "</option>")
subrs.movenext
wend
end if
end if
'關閉所有的記錄集
subrs.close
set subrs = nothing
rst2.close
set rst2 = nothing
%>
</select>
</form>
</body>
</html>