下拉框连动的小例子(数据库版)

ASP文件:

1@ Language=VBScript 
1Option Explicit
1<html>
2<head>
3<title>List</title>
4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>   

dim conn
dim rs
dim sql
dim count
dim rs1
dim sql1

set conn = server.CreateObject ("adodb.connection")
conn.Open "test","sa",""

sql = "select * from district order by locationid asc"
set rs = conn.execute(sql)

1<script language="JavaScript">   
2var onecount;   
3onecount=0;   
4subcat = new Array();   

count = 0
do while not rs.eof

1subcat[```
2=count
3```] = new Array("```
4= trim(rs("districtname"))
5```","```
6= trim(rs("locationid"))
7```","```
8= trim(rs("districtid"))
9```");   

count = count + 1
rs.movenext
loop
rs.close
set rs=nothing

 1onecount=```
 2=count
 3```;   
 4  
 5function changelocation(locationid)   
 6{   
 7document.myform.smalllocation.length = 0;   
 8  
 9var locationid=locationid;   
10var i;   
11document.myform.smalllocation.options[0] = new Option('====所有地区====','');   
12for (i=0;i < onecount; i++)   
13{   
14if (subcat[i][1] == locationid)   
15{   
16document.myform.smalllocation.options[document.myform.smalllocation.length] = new Option(subcat[i][0], subcat[i][2]);   
17}   
18}   
19  
20}   
21</script>
22</head>
23<body>
24<form method="post" name="myform">
25<select name="biglocation" onchange="changelocation(document.myform.biglocation.options[document.myform.biglocation.selectedIndex].value)">   

sql1 = "select * from location order by locationname asc"
set rs1 = conn.Execute (sql1)
do while not rs1.eof

1<option ```"="" locationid"))="" value="```
2=trim(rs1(">```
3=trim(rs1("locationname"))
4```</option>   

rs1.movenext
loop
rs1.close
set rs1 = nothing
conn.Close
set conn = nothing

 1</select>
 2<select name="smalllocation">
 3<option selected="" value="">==所有地区==</option>
 4</select>
 5</form>
 6<script language="javascript">   
 7changelocation(document.myform.biglocation.options[document.myform.biglocation.selectedIndex].value);   
 8</script>
 9</body>
10</html>

数据库:
create table location
(

locationid char(2) not null primary key,
locationname char(50) not null

)

create table district
(

districtid char(3) not null primary key,
districtname char(50) not null,
locationid char(2) not null
)

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