关于SELECT的无限级联(省|市|县|乡|村|...)

index.asp
程序代码:

 1<html>
 2<head>
 3<title></title>
 4<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
 5<style type="text/css"><!--   
 6FONT{font-size:12px}   
 7TD{font-size:12px}   
 8A{color:#333399}   
 9A:hover{color:#FF6600}   
10\--></style>
11<script language="JavaScript" type="text/JavaScript"><!--   
12function funOnload(){   
13document.all.list_file.src="jscript_city.asp?base=0&sele=0-&elem=Select01";   
14} 
15
16//pBase级数,以0基,   
17//pSele是<option 的value值   
18//pEle是下一级的表单名字   
19function chgSelect(pBase,pSele,pElem){   
20//当改变了一个列表之后。清除以后的列表的值。   
21for(i=parseInt(pElem.replace("Select",""));i<=5;i++){   
22var tmp="000"+i;   
23var pElem1=eval("document.Form1.Select"+tmp.substr(tmp.length-2)); 
24
25pElem1.length=1;   
26pElem1.selectedIndex=0;   
27} 
28
29//JS用ASP得到数据库的数据来更新下级列表   
30document.all.list_file.src="jscript_city.asp?base="+pBase+"&sele="+pSele+"&elem="+pElem;   
31} 
32
33//--></script>
34<script id="list_file" language="JavaScript" src="" type="text/JavaScript"></script>
35</head>
36<body bgcolor="#FFFFFF" leftmargin="0" onload="funOnload();" text="#333333" topmargin="2">
37<form name="Form1">
38<select name="Select01" onchange="chgSelect(1,this.options[this.selectedIndex].value,'Select02')">
39<option value="">省...</option>
40</select>
41<select name="Select02" onchange="chgSelect(2,this.options[this.selectedIndex].value,'Select03')">
42<option value="">市...</option>
43</select>
44<select name="Select03" onchange="chgSelect(3,this.options[this.selectedIndex].value,'Select04')">
45<option value="">县...</option>
46</select>
47<select name="Select04" onchange="chgSelect(4,this.options[this.selectedIndex].value,'Select05')">
48<option value="">乡...</option>
49</select>
50<select name="Select05">
51<option value="">村...</option>
52</select>
53</form>
54</body></html>

'jscript_city.asp
程序代码:

 1   
 2' varBase 下拉菜单等级   
 3' varSele 所选择下拉菜单项的数据库ID   
 4' varElem 下一级的表单名称 
 5
 6varBase=Request.QueryString("base")   
 7varSele=Left(Request.QueryString("sele"),InStr(Request.QueryString("sele"),"-")-1)   
 8varElem=Request.QueryString("elem") 
 9
10varDistName=""   
11varAutoID="" 
12
13Set conDB=Server.CreateObject("ADODB.CONNECTION")   
14conDB.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ="&Server.Mappath("db1.mdb") 
15
16sqlCommand="select * from table1 where filed1="&varBase&" and filed2="&varSele   
17Set rsRecord=conDB.Execute(sqlCommand) 
18
19While Not rsRecord.eof   
20varDistName=varDistName&chr(34)&rsRecord("filed3")&chr(34)   
21varAutoID=varAutoID&chr(34)&rsRecord("id")&chr(34) 
22
23rsRecord.movenext   
24If Not rsRecord.Eof Then   
25varDistName=varDistName&","   
26varAutoID=varAutoID&","   
27End If   
28Wend 
29
30Response.Write("var varDistName=new Array("&varDistName&")"&vbcrlf)   
31Response.Write("var varAutoID=new Array("&varAutoID&")"&vbcrlf) 
32
33Response.Write("var varElem=eval("&chr(34)&"document.Form1."&varElem&chr(34)&")"&vbcrlf) 
34
35Response.Write("varElem.length=varDistName.length+1;"&vbcrlf) 
36
37Response.Write("for(var i=0;i<varDistName.length;i++){"&vbcrlf)   
38Response.Write(" varElem.options[i+1].text=varDistName;"&vbcrlf)   
39Response.Write(" varElem.options[i+1].value=varAutoID+'-'+varDistName;"&vbcrlf)   
40Response.Write("}"&vbcrlf) 
41
42Response.Write("varElem.selectedIndex=0;"&vbcrlf) 

db1.mdb
'-------------------------------------------------
福建 厦门 思明 黄厝 曾厝桉村
福建 泉州 丰泽 西湖 水头村

如上面的五级转成数据库为:
'-------------------------------
id filed1 filed2 filed3
1 0 0 福建
2 1 1 厦门
3 1 1 泉州
4 2 2 思明
5 3 4 黄厝
6 4 5 曾厝桉村
7 2 3 丰泽
8 3 7 西湖
9 4 8 水头村
'-------------------------------------------------
数据库说明:
id 自动编号
filed1 下拉菜单列表的等级(看级数。可以设置它的精度。是数字类型)
filed2 上一级的id号(用长整型吧)
filed3 这个就不用说了吧(文件。长度自己看情况)

本程序在IIS4+WIN2000P+ACCESS2000下通过。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus