这样的问题很多.你应该先学会搜一搜.这是我前几天写的,IE,nescape,mozilla下通过
<!--#include file="conn.inc"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<title>两级关联菜单</title>
</HEAD>
<BODY>
<!--
by fason(2003.4.5)
表名:pro_city
结构:id(自编) province(文本) city(文本)
注释:conn.inc为连接数据库文件
ie,netscape,mozilla下测试通过
-->
<SCRIPT LANGUAGE=javascript>
<!--
var prv=new Array()//省
var cty=new Array()//市
function init(){
<%
dim prv,cty,i,j
i=0
set prv=conn.execute("select distinct province from pro_city")
if not prv.eof then
do while not prv.eof
j=0
%>
prv[<%=i%>]="<%=trim(prv("province"))%>";
<%
set cty=conn.execute("select city from pro_city where province='"&trim(prv("province"))&"'")
if not cty.eof then%>
cty[<%=i%>]=new Array()
<%do while not cty.eof%>
cty[<%=i%>][<%=j%>]="<%=trim(cty("city"))%>"
<%j=j+1
cty.movenext
loop
end if
cty.close :set cty=nothing
i=i+1
prv.movenext
loop
end if
prv.close :set prv=nothing
%>
if(prv.length >0){
for(i=0;i<prv.length;i++)
with(document.frm.province)options[length]=new Option(prv[i],prv[i])
document.frm.province.onchange=new Function("go(this.selectedIndex)")
}
}
Sql_All = "Select Id,Name from prover"
set Rs_All = Server.CreateObject ("adodb.recordset")
Rs_All.Open Sql_All,conn,3,1
'动态生成一个内容为省的select
Response.Write "<Select name = Province id = Province onchange = ChangeProvince(this)>"
do while not Rs_All.EOF
%>
<option value = <%=Rs_All("Id")%>><%=Rs_All("Name")%>
<%
Rs_All.MoveNext
loop
Response.Write "</Select>"
'到此第一个select为止
CloseRs(Rs_All)
'在城市表city中选择所有记录。Id:城市Id;CId:省Id;Name:城市的名字。
'然后按照:省Id加上“,”加上城市Id加上“,”加上城市的名字组合成一个字符串;
'如:'1,2,济南',表示:省的Id是1,城市的Id是2,城市的名字是 济南。
'然后每一个类似的字符串作为一个一维数组的一个元素的值。
'如此循环,动态生成了一个数组,每一个下表的值为类似“1,2,济南”的字符串
Sql_City= "select Id,CId,Name from city order by Id"
set Rs_City = server.CreateObject ("adodb.recordset")
Rs_City.Open Sql_City,conn,3,1
count = Rs_City.RecordCount '得到动态数组的元素个数
redim Arr_City(count) '重新初始化动态数组
i = 0
do while not Rs_City.EOF
New_Str = ""
theId = Rs_City("Id") '得到城市Id
theCId = Rs_City("CId") '得到省的Id
theName = Rs_City("Name") '得到城市的名字
New_Str = theCId&","&theId&","&theName '组合成一个字符串
Arr_City(i) = New_Str '给数组赋值
i = i +1
Rs_City.MoveNext
loop
'动态数组的创建到此为止
CloseRs(Rs_City)
Conn.Close
set Conn = Nothing
'然后把上边动态生成的数组Arr_City赋值给javascipt中的一个数组:All_City
Response.Write "<script language = javascript>"
Response.Write " var All_City = new Array("&count&");"
for i = lbound(Arr_City) to ubound(Arr_City)-1
Response.Write "All_City["&i&"] = '"&Arr_City(i)&"';"
next
Response.Write "</script>"
'数组赋值到此为止
'产生一个空的select,用来存放动态生成的城市
%>
<select name = City id = City>
</select>
<% '到此为止 %>
</body>
<script language = javascript>
function ChangeProvince(id)
{
theProvinceId = id.options[id.selectedIndex].value; //得到所选择省的Id
for (j = document.all.City.length;j>=0;j--) //去掉原来城市select中的城市
document.all.City.remove(j);
for (i = 0 ;i<<%=count%>;i++)
{
theCity = All_City[i].split(","); //利用函数split生成一个数组theCity,内容类似为("1","2","济南")
if (theProvinceId == theCity[0])
{
document.all.City.options.add(new Option(theCity[2])); //第二个select动态增加内容
}
}