無限級聯動菜單-AJAX版(附源碼下載)

來源:互聯網
上載者:User

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta content="琥珀[hopesoft],http://www.10090.com" name="author">
    <title>琥珀網 - 無限級聯動菜單[AJAX版]</title>
    <style type="text/css">
    body, td
    {
      font-family: 宋體;
      font-size: 12px;
    }
    </style>
<script language="JavaScript">
function GetResult(str,classid)
{
/*
*--------------- GetResult(str,classid) -----------------
* GetResult(str)
* 功能:通過XMLHTTP發送請求,返回結果.
* 參數:str,字串,發送條件;classid,數字,菜單層級
* 執行個體:GetResult(document.all.userid.value,1);
*--------------- GetResult(str,classid) -----------------
*/
//定義菜單級數,菜單ID數組,菜單對應欄位數組
var MenuIdArr,MenuFieldArr,MenuClass
MenuIdArr= new Array()
MenuFieldArr=new Array()
MenuClass=4

MenuIdArr[1]="sel1"
MenuIdArr[2]="sel2"
MenuIdArr[3]="sel3"
MenuIdArr[4]="sel4"

MenuFieldArr[1]="name"
MenuFieldArr[2]="name"
MenuFieldArr[3]="name"
MenuFieldArr[4]="name"

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
}

var linkurl="ajax_server.asp?classid="+classid+"&sel="+str+"&fieldname="+MenuFieldArr[classid+1]
http_request.open("GET",linkurl,false);
http_request.send(null);

//伺服器端處理返回的是經過escape編碼的字串.
//在頁面顯示伺服器查詢結果

var returntxt=unescape(http_request.responseText)
if(returntxt.length>0)
{document.all,ajax.innerHTML="伺服器返回結果:<font color='red'>"+returntxt+"</font>   [Powered by AJAX]"}
else
{document.all,ajax.innerHTML=""}

//通過XMLHTTP返回資料,開始構建Select.
//BuildSel(returntxt,eval("document.all."+MenuIdArr[classid+1]))
BuildSel(returntxt,document.getElementById(MenuIdArr[classid+1]))

//============更改下下級以下菜單為空白==============
var kkk
for(kkk=classid+2;kkk<=MenuClass;kkk++)
 {  
 submenu=document.getElementById(MenuIdArr[kkk]) 
 submenu.length=1
 submenu.options[0].selected=true
 }
}

function BuildSel(str,sel)
{
/*
*--------------- BuildSel(str,sel) -----------------
* BuildSel(str,sel)
* 功能:通過str構建Select.
* 參數:str,字串,由服務端返回的.有特定結構"字串1|,字串2,字串3"
*          也可為"字串1序號|字串1文本,字串2序號|字串2文本,字串3序號|字串3文本",如本例
* 參數:sel,要構建的Select
* 執行個體:BuildSel(unescape(oBao.responseText),document.all.sel2)
*--------------- BuildSel(str,sel) -----------------
*/
//先清空原來的資料.
sel.options.length=0;
var arrstr = new Array();
arrstr = str.split(",");
//開始構建新的Select.
sel.options.add(new Option( "-----請選擇-----","")); 
if(str.length>0)  
{
for(var i=0;i<arrstr.length;i++)
{
//分割字串
var subarrstr=new Array
subarrstr=arrstr[i].split("|")
//產生下級菜單
sel.options.add(new Option(subarrstr[1],subarrstr[0]));
}
sel.options[0].selected=true
}

}
</script>

    <form name="form1" method="post" action="">
        <table width="90%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
          <tr bgcolor="F1F1F1">
            <td height="24" colspan="2" align="center">琥珀無限級聯動菜單-AJAX版 [HPMenu_AJAX V1.0]</td>
          </tr>
          <tr bgcolor="#FFFFFF">
            <td width="12%" height="24" align="center">所 在 洲:</td>
            <td><select name="sel1" id="sel1" onChange="GetResult(this.value,1)">
               <option value="" selected>-----請選擇-----</option>
              <option value="10">亞洲</option>
              <option value="11">歐洲</option>
            </select></td>
          </tr>
          <tr bgcolor="#FFFFFF">
            <td height="24" align="center">國  家:</td>
            <td><select name="sel2" id="sel2" onChange="GetResult(this.value,2)">
               <option value="" selected>-----請選擇-----</option>
            </select></td>
          </tr>
          <tr bgcolor="#FFFFFF">
            <td height="24" align="center">城  市:</td>
            <td><select name="sel3" id="sel3" onChange="GetResult(this.value,3)">
               <option value="" selected>-----請選擇-----</option>
            </select></td>
          </tr>
          <tr bgcolor="#FFFFFF">
            <td height="24" align="center">地  區:</td>
            <td><select name="sel4" id="sel4">
               <option value="" selected>-----請選擇-----</option>
            </select></td>
          </tr>
          <tr bgcolor="F1F1F1">
            <td height="24" colspan="2" align="center" id="ajax">&nbsp;</td>
          </tr>
   </table>
     <br>
     <table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="25" align="left">&nbsp;</td>
          </tr>
          <tr>
            <td height="25" align="left">&nbsp;</td>
          </tr>
          <tr>
            <td height="25" align="left">&nbsp;</td>
          </tr>
          <tr>
            <td height="25" align="left">&nbsp;</td>
          </tr>
          <tr>
            <td height="25" align="left">&nbsp;</td>
          </tr>
          <tr>
            <td height="50" align="center">&nbsp;</td>
          </tr>
        </table>
        <script language="javascript">
  GetResult(document.getElementById("sel1").value,1)       
        </script>
</form>
</body>
</html>

 原始碼下載:點擊下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.