::烟舞者::

   

------------------------------------------

-=努力学习后台编程=-

 
动态级联菜单
作者:半支烟  发表时间:2005-3-10

 

这是一个带有数据库的动态级联菜单,搞了好久都没搞出来,在经典有人帮我贴出来了,我稍微修改了一下收藏在这里,希望和大家一起分享!

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="Connections/connMenu.asp" -->
<%
Dim RecMain
Dim RecMain_numRows

Set RecMain = Server.CreateObject("ADODB.Recordset")
RecMain.ActiveConnection = MM_connMenu_STRING
RecMain.Source = "SELECT * FROM main"
RecMain.CursorType = 0
RecMain.CursorLocation = 2
RecMain.LockType = 1
RecMain.Open()

RecMain_numRows = 0
%>
<%
Dim RecSub
Dim RecSub_numRows

Set RecSub = Server.CreateObject("ADODB.Recordset")
RecSub.ActiveConnection = MM_connMenu_STRING
RecSub.Source = "SELECT * FROM sub"
RecSub.CursorType = 0
RecSub.CursorLocation = 2
RecSub.LockType = 1
RecSub.Open()

RecSub_numRows = 0
%>
<html>
<head>
<title>动态级联菜单</title>
</head>

<body>
<form name="form1">
  <select name="MainMenu" id="MainMenu" onchange="redirect(this.options.value)">
    <option value="0">--请选择--</option>
    <%
While (NOT RecMain.EOF)
%>
    <option value="<%=(RecMain.Fields.Item("main_id").Value)%>"><%=(RecMain.Fields.Item("main_name").Value)%></option>
    <%
  RecMain.MoveNext()
Wend
If (RecMain.CursorType > 0) Then
  RecMain.MoveFirst
Else
  RecMain.Requery
End If
%>
  </select>
  <select name="SubMenu" id="SubMenu">
    <option value="0">--请选择--</option>
  </select>
</form>
<script>
<!--
var temp=document.form1.SubMenu
function redirect(x){
var i
i=0
<%while not RecSub.eof
%>
	cnt=<%=RecSub("main_id")%>
	
	if (cnt==x){
	temp.options[i]=new Option("<%=RecSub("sub_name")%>","<%=RecSub("sub_id")%>")
	i=i+1
	}
<%
RecSub.movenext
Wend
%>
	}
-->
</script>
</body>
</html>
<%
RecMain.Close()
Set RecMain = Nothing
%>
<%
RecSub.Close()
Set RecSub = Nothing
%>


看上去代码好像不少,其实大部分是DW中的"绑定"和"服务器行为"自动生成的.简单实用!

补充:上面的代码存在很多不足
下面是一个更好一点的代码
<%dim count
set rs=server.createobject("adodb.recordset")
rs.open "select * from shop_Nclass order by Nclassidorder",conn,1,1%>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<%
   count = 0
   do while not rs.eof 
%>
subcat[<%=count%>] = new Array("<%= trim(rs("Nclass"))%>","<%= rs("anclassid")%>","<%= rs("Nclassid")%>");
<%
        count = count + 1
        rs.movenext
        loop
        rs.close
%>
		
onecount=<%=count%>;

function changelocation(locationid)
    {
    document.myform.Nclassid.length = 0; 

    var locationid=locationid;
    var i;
    for (i=0;i < onecount; i++)
        {
            if (subcat[i][1] == locationid)
            { 
             document.myform.Nclassid.options[document.myform.Nclassid.length] = new Option(subcat[i][0], subcat[i][2]);
            }        
        }
        
    }    
</script>

<%  rs.open "select * from shop_anclass order by anclassidorder",conn,1,1
if rs.eof and rs.bof then
	response.write "请先添加栏目。"
	response.end
else %>
              大类 
              <select name="anclassid" size="1" id="anclassid" onChange="changelocation(document.myform.anclassid.options[document.myform.anclassid.selectedIndex].value)" class="wenbenkuang">
                <option selected value="<%=rs("anclassid")%>"><%=trim(rs("anclass"))%></option>
<% dim selclass
   selclass=rs("anclassid")
   rs.movenext
   do while not rs.eof %>
                <option value="<%=rs("anclassid")%>"><%=trim(rs("anclass"))%></option>
<%    rs.movenext
   loop
end if
rs.close
%>
             </select>
            小类 
             <select name="Nclassid" class="wenbenkuang">
<% rs.open "select * from shop_Nclass where anclassid="&selclass ,conn,1,1
if not(rs.eof and rs.bof) then %>
                <option selected value="<%=rs("NclassID")%>"><%=rs("Nclass")%></option>
<%  rs.movenext
	do while not rs.eof%>
                <option value="<%=rs("NclassID")%>"><%=rs("Nclass")%></option>
    <% rs.movenext
	loop
end if
rs.close
%>

----------------------------------------
 

 

半支烟发表于:ASP  


 
留言内容(共有条)


{CommentAuthor}评论说:
{CommentContent}

--- {CommentTime} {CommentUrl}


 

流年--似水


也说--几句


 来来--往往


 

 快速--登陆

*用户名:
*密   码:

 


日志--搜索


踩上--几脚

 
zzj 最新的 20 条日志
 

 

 

Designed By UBABY--2005