ajax asp的二級聯動菜單一實例

來源:互聯網
上載者:User

category.asp檔案

<%
dim conn,rs
dim connstr
dim sqlcmd
'建立資料庫教程連線物件並開啟
set conn=server.createobject("adodb.connection")
connstr="provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("data.mdb")
conn.open connstr
'用於從資料庫中擷取資料的sql語句
sqlcmd="select id,name from category where level=0"
'建立資料集對象
set rs=conn.execute(sqlcmd)
%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>二級菜單樣本</title>
</head>
<body>
<form name="form1">
 <select name="select1" onchange="getlevel2()">
  <option value="0">請選擇一級分類:</option>
  <%
   '遍曆記錄集產生下拉式功能表選項
   while not rs.eof
    response.write("<option value='" & rs("id") & "'>")
    response.write(rs("name"))
    response.write("</option>")
    rs.movenext
   wend
   '關閉資料庫連接及記錄集,釋放資源www.111cn.net
   rs.close
   conn.close
   set rs=nothing
   set conn=nothing
  %>
 </select>
 <select name="select2">
  <option value="0">請選擇二級分類:</option>
 </select>
</form>
</body>
</html>
<script language="網頁特效" type="text/javascript">
<!--
//cache用於儲存已經擷取的資料
var cache=[];
function getlevel2(){
 if(document.forms.form1.select1.selectedindex==0){
  //當一級菜單未選中時,二級菜單僅保留提示項
  document.forms.form1.select2.length=1;
  return;
 }
 //如果當前二級分類沒有被緩衝,則從伺服器端擷取
 if(!cache[document.forms.form1.select1.selectedindex]){
  //建立跨瀏覽器的xmlhttprequest對象
  var xmlhttp;
  try{
   xmlhttp= new activexobject('msxml2.xmlhttp');
  }catch(e){
   try{
    xmlhttp= new activexobject('microsoft.xmlhttp');
   }catch(e){
    try{
     xmlhttp= new xmlhttprequest();
    }catch(e){}
   }
  }
  //建立請求,並使用escape對username編碼,以避免亂碼
  xmlhttp.open("get","ajax.asp?id="+document.forms.form1.select1.value);
  xmlhttp.onreadystatechange=function(){
   if(xmlhttp.readystate==4){
    if(xmlhttp.status==200){
     cache[document.forms.form1.select1.selectedindex]=eval("("+unescape(xmlhttp.responsetext)+")");
     //擷取成功後重新調用getlevel2,www.111cn.net將資料填充到下拉框,如果直接在這裡寫會造成代碼重複
     getlevel2();
    }else{
     alert("網路失敗。");
    }
   }
  }
  xmlhttp.send(null);
  //必須在這裡返回,等待回調
  return;
 }
 //此時已經確保緩衝不為空白
 document.forms.form1.select2.length=1;
 var _arr=cache[document.forms.form1.select1.selectedindex];
 for(var i=0;i<_arr.length-1;i+=2){
  with(document.forms.form1.select2){
   options[options.length]=new option(_arr[i],_arr[i+1]);
  }
 }
}
//-->
</script>

ajax.asp

<%
dim conn,rs
dim connstr
dim sqlcmd

'建立資料庫連接對象並開啟
set conn=server.createobject("adodb.connection")
connstr="provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("data.mdb")
conn.open connstr

'擷取相應的二級目錄資料
sqlcmd="select id,name from category where level=1 and parentid=" & request.querystring("id")
set rs=conn.execute(sqlcmd)

'返回javascript格式的數組
response.write("[")
while not rs.eof
 '使用escape避免亂碼問題
 response.write("'" & escape(rs("name")) & "', ")
 response.write(rs("id"))
 response.write(",")
 rs.movenext
wend
'為了避免最後一個逗號的問題,最後一個數組元素不使用
response.write("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.