function CheckLm(thevalue){ var byid=new Array(); var cotename=new Array(); var id=new Array(); var i; var id_s; var byid_s; var cotename_s; var thevalue_r; var data='<select name="lm" onchange="CheckLm(this.value)">' id_s="|"; cotename_s='-選擇-|'; <%set rs=server.CreateObject("adodb.recordset") sql="select * from [DB_Cote] Order by byid,id" rs.open sql,conn,1,1 i=0 if not rs.eof then do while not rs.eof response.Write("id["&i&"]="&rs("id")&";"&chr(10)) response.Write("byid["&i&"]="&rs("byid")&";"&chr(10)) response.Write("cotename["&i&"]='"&rs("cotename")&"';"&chr(10)) rs.movenext i=i+1 loop else response.Write("id[0]=0;byid[0]=0;cotename[0]='無分類';") end if rs.close set rs=nothing%> if(thevalue==null||thevalue==undefined||thevalue==''){thevalue=0;} //if(lmvalue==null||lmvalue==undefined||lmvalue==''){lmvalue=0;} for(k=0;k<id.length;k++){ if(thevalue==id[k]){id_s+=thevalue+"|";cotename_s+=cotename[k]+"|";thevalue_r=byid[k];break;}else{thevalue_r=0;} } for(i=0;i<id.length;i++){ if(thevalue==byid[i]){ id_s+=id[i]+"|";cotename_s+=cotename[i]+"|"; } } if(id_s=='-1|'){ for(i=0;i<id.length;i++){ if(thevalue==id[i]){ id_s+=id[i]+"|";cotename_s+=cotename[i]+"|"; } } } id_v=id_s.split("|"); cotename_v=cotename_s.split("|"); var length=id_v.length-1; for(j=0;j<length;j++){ if(thevalue==id_v[j]){ data+="<option value='"+id_v[j]+"' selected>"+cotename_v[j]+"</option>";} else{ data+="<option value='"+id_v[j]+"'>"+cotename_v[j]+"</option>"; } } if(thevalue!=0){data+="<option value='"+thevalue_r+"'>上級分類</option>";} //alert(length+'/'+thevalue); var lm=document.getElementById('lmid'); lm.innerHTML=data+"</select>"; } <%Conn.Close:Set Conn=Nothing%>
調用頁面代碼 復制代碼 代碼如下: <% Dim lm lm=request("lm") if lm=Empyt or Not Isnumeric(lm) then lm=0 %> <html> <script type="text/javascript" src="*.asp"></script> <body onLoad="CheckLm(<%=lm%>)"> <div id="lmid"></div> </body> </html>
運行後結果
[Ctrl+A 全選 注:如需引入外部Js需刷新才能執行] 數據庫:
遞歸 復制代碼 代碼如下: Sub GetA(TheID)
set rs=server.CreateObject("adodb.recordset") dim rs,sql sql="select * from [DB_Cote] Where ByID in("&TheID&")" rs.open sql,conn,1,1 i=0 if not rs.eof then do while not rs.eof response.Write(rs("cotename")&"<br>") call GetA(Rs("id")) rs.movenext i=i+1 loop end if rs.close set rs=nothing