利用ASP嵌套JS+SQL Server打造兩級連動下拉框(二)

來源:互聯網
上載者:User
js|server|下拉 利用ASP嵌套JS+SQL Server打造兩級連動下拉框(2) TwoLevel.asp<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="GENERATOR" content="Microsoft FrontPage 4.0"><meta name="ProgId" content="FrontPage.Editor.Document"><title>下拉式清單樣本</title></head>       <%      dim conn         dim connstr      on error resume next      set conn=server.CreateObject("ADODB.connection")      connstr="driver={SQL Server};server=wen;uid=sa;pwd=;database=DB_ProvinceCat"      conn.Open connstr           set Rs=server.createobject("ADODB.recordset")      Rs.CursorLocation = adUseClient        SQL="Select * from Tb_Province"      Rs.open SQL,conn,1,1      if Not Rs.Eof then          Dim strJScript          strJScript=""          Response.Write("<SCRIPT language=JavaScript>")&chr(13)          Response.Write("function setcategory(S){")&chr(13)          Response.Write("S.category.length="+Cstr(Rs.Recordcount+1)+";")&chr(13)          dim i          i=0          While Not Rs.Eof               strJScript=strJScript+"S.category.options["+CStr(i)+"].value="+chr(34)+Trim(Rs("ProvinceCode"))+chr(34)+";"+chr(13)               strJScript=strJScript+"S.category.options["+CStr(i)+"].text="+chr(34)+Trim(Rs("ProvinceName"))+chr(34)+";"+chr(13)                 i=i+1             Rs.MoveNext         Wend             Response.write(strJScript)             Response.Write("S.category.options["+CStr(i)+"].value="+chr(34)+"無"+chr(34)+";"+chr(13))             Response.Write("S.category.options["+CStr(i)+"].text="+chr(34)+"**請選擇省份**"+chr(34)+";"+chr(13))             Response.Write("S.category.options["+Cstr(i)+"].selected=true;"+chr(13))             Response.Write("}")&chr(13)             Rs.Close             Set Rs=Nothing       end if       set Rs=server.createobject("ADODB.recordset")      Rs.CursorLocation = adUseClient       SQL="Select Num=Count(*),Tb_Province.ProvinceCode From Tb_Province,Tb_City where Tb_Province.ProvinceCode=substring(Tb_City.CityCode,1,2)  group by Tb_Province.ProvinceCode order by Tb_Province.ProvinceCode"      Rs.open SQL,conn,1,1      if Not Rs.Eof then                   dim Num(30),j          j=0              Do While Not Rs.Eof                  Num(j)=Rs("Num")                   j=j+1                  Rs.MoveNext              Loop       end if      Rs.Close      set Rs=Nothing      SQL=""      set Rs=server.createobject("ADODB.recordset")      Rs.CursorLocation = adUseClient       SQL="Select Tb_Province.*,Tb_City.* From Tb_Province,Tb_City where Tb_Province.ProvinceCode=substring(Tb_City.CityCode,1,2) order by Tb_Province.ProvinceCode"      Rs.open SQL,conn,1,1       j=0      if Not Rs.Eof then          Response.Write("function setsmall_cat(D){")&chr(13)          Response.Write("var valuecategory=D.category.options[D.category.selectedIndex].value;")&chr(13)          Response.write("if(valuecategory.indexOf("+chr(34)+"無"+chr(34)+")==0){"+chr(13)&_                             "D.small_cat.length=1;"&_                             "D.small_cat.options[0].value="+chr(34)+"無"+chr(34)+";"+chr(13)&_                             "D.small_cat.options[0].text="+chr(34)+"**請選擇城市**"+chr(34)+chr(13)&_                             "D.small_cat.options[0].selected=true;"+chr(13)&_                              "}")                                        i=0          dim NextLevel,UpCatCode,overflow          NextLevel="N"          overflow="N"          strJScript=""        Do While Not Rs.Eof            UpcateCode=Trim(Rs("ProvinceCode"))              if  NextLevel="N" then              strJScript="else if(valuecategory.indexOf("+chr(34)+UpcateCode+chr(34)+")==0){"+chr(13)              strJScript=strJScript+"D.small_cat.length="+Cstr(Num(j))+";"+chr(13)              j=j+1              NextLevel="Y"           end if                   strJScript=strJScript+"D.small_cat.options["+CStr(i)+"].value="+chr(34)+Trim(Rs("CityCode"))+chr(34)+";"+chr(13)               strJScript=strJScript+"D.small_cat.options["+CStr(i)+"].text="+chr(34)+Trim(Rs("CityName"))+chr(34)+";"+chr(13)                           i=i+1                        Rs.MoveNext                      if UpcateCode<>Trim(Rs("ProvinceCode")) then                overflow="Y"           end if               if  overflow="Y" then                 Response.write(strJScript+"}")                  strJScript=""                 overflow="N"                 NextLevel="N"                 i=0            end if         Loop                          Response.Write("}</Script>")&chr(13)              Rs.Close             Set Rs=Nothing       end if    Conn.close    set conn=nothing  %>     <body onload=setcategory(document.select);setsmall_cat(document.select)><Form name="select" method="POST" action="#"><center><Table><TR>                                                                               <td>請選擇:</td>  <TD><SELECT  onchange=setsmall_cat(document.select) name=category size="1" ></SELECT> <SELECT  name=small_cat size="1" ></SELECT> </TD></TR></Table></center></Form></body></html>第三,進行代碼調試。本程式在WIN2000+IIS+SQL Server7.0環境下調試通過。                                     (全文完)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.