<!--月影飞鸿2003-05-25 晚-->
<!--#include file = dbconn.asp-->
<body onload =" ChangeProvince(document.all.Province);aa.window.close();">
<%
  Dim Arr_City
  
  sub CloseRs(rs)
    Rs.close
    set Rs = nothing
  end sub
  
  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 "<center>"
  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>
<% '到此为止 %>
</center>
</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动态增加内容       
      }
    }
    
  }
  
</script>