在“易久乐网店”程序中我拟划分了二级分类,第一级为“商品目录”,第二级为“商品分类”。想实现二级菜单联动,并且能在同一页面中分别查看所有商品以及大目录或小商品类下的相关商品。看了很多关于二级联动的代码,总觉得加上 ASP+JS 大段的代码,显得太过复杂,并且结合实际所需要做到的参数传递,传递也不是很方便。所以,想了想用了自己一个笨一些的,但或许却是最简单的办法:
实现方式:
A,对应目录和分类,分别设置一个参数:“bigid”;“smaid”;
B,接收“bigid” 为目录、分类共享的判断参数。(因为分类中有“bigid”字段与目录 ID 关联)
C,选择目录时 URL 为:?bigid=*。选择分类时:?bigid=*&smaid=*。
D,用接收参数判断菜单被选择。
E,只传递一个“bigid”,即可实现二级联动了。(传两个是为了方便了不同目录、分类查询商品)
代码试例:
<!--#include file="conn.asp" -->
< %
Dim urlid,smaid
if Trim(Request.QueryString("bigid"))<>"" then
urlid=Trim(Request.QueryString("bigid"))
end if
if Trim(Request.QueryString("smaid"))<>"" then
smaid=Trim(Request.QueryString("smaid"))
end if
set rs_bigclass=server.createobject("adodb.recordset")
sql="select * from Bigclass order by id desc"
rs_bigclass.open sql,conn,1,1
set rs_smallclass=server.createobject("adodb.recordset")
sql="select * from Smallclass Where bigid="&urlid&" order by id desc"
rs_smallclass.open sql,conn,1,1
%>
< %
Dim urlid,smaid
if Trim(Request.QueryString("bigid"))<>"" then
urlid=Trim(Request.QueryString("bigid"))
end if
if Trim(Request.QueryString("smaid"))<>"" then
smaid=Trim(Request.QueryString("smaid"))
end if
set rs_bigclass=server.createobject("adodb.recordset")
sql="select * from Bigclass order by id desc"
rs_bigclass.open sql,conn,1,1
set rs_smallclass=server.createobject("adodb.recordset")
sql="select * from Smallclass Where bigid="&urlid&" order by id desc"
rs_smallclass.open sql,conn,1,1
%>
<form name="menus" method="post" action="">
<table width="400" border="0" cellpadding="0" cellspacing="2" class="tr01">
<tr>
<td width="80" class="title01" align="center">选择商品:</td>
<td width="160" align="right">
< %
Response.Write("<select name=""bigclass"" onChange=""if(this.selectedIndex && this.selectedIndex!=0){window.location.href(this.value);}this.selectedIndex=0;"">")
Response.Write("<option value=>请选择商品目录</option>")
Do while not rs_bigclass.eof
Response.Write("[color=Red]<option value<strong>=?bigid="&rs_bigclass("id")&"") '列出该目录下所有商品。
if CStr(rs_bigclass("id"))=urlid then
Response.Write(" selected=selected")
end if
Response.Write(">"&rs_bigclass("name")&"</option>")
rs_bigclass.movenext
loop
Response.Write("")
Response.Write("</td><td width=""160"">")
Response.Write("<sel ect name=""smallclass"" onChange=""if(this.selectedIndex && this.selectedIndex!=0){window.location.href(this.value);}this.selectedIndex=0;"">")
Response.Write("<option value=>请选择商品分类</option>")
If urlid<>"" then
Do while not rs_smallclass.eof
Response.Write("[color=Red]<option value=?<strong>bigid="&urlid&"&smaid="&rs_smallclass("id")&" ") '列出该分类下所有商品
if CStr(rs_smallclass("id"))=smaid then
Response.Write(" selected=selected")
end if
Response.Write(">"&rs_smallclass("name")&"</option>")
rs_smallclass.movenext
loop
End if
Response.Write("</sel>")
%></td>
</tr>
</table>
</form>
< %
rs_bigclass.close()
rs_smallclass.close()
Set conn=Nothing
%>
<table width="400" border="0" cellpadding="0" cellspacing="2" class="tr01">
<tr>
<td width="80" class="title01" align="center">选择商品:</td>
<td width="160" align="right">
< %
Response.Write("<select name=""bigclass"" onChange=""if(this.selectedIndex && this.selectedIndex!=0){window.location.href(this.value);}this.selectedIndex=0;"">")
Response.Write("<option value=>请选择商品目录</option>")
Do while not rs_bigclass.eof
Response.Write("[color=Red]<option value<strong>=?bigid="&rs_bigclass("id")&"") '列出该目录下所有商品。
if CStr(rs_bigclass("id"))=urlid then
Response.Write(" selected=selected")
end if
Response.Write(">"&rs_bigclass("name")&"</option>")
rs_bigclass.movenext
loop
Response.Write("")
Response.Write("</td><td width=""160"">")
Response.Write("<sel ect name=""smallclass"" onChange=""if(this.selectedIndex && this.selectedIndex!=0){window.location.href(this.value);}this.selectedIndex=0;"">")
Response.Write("<option value=>请选择商品分类</option>")
If urlid<>"" then
Do while not rs_smallclass.eof
Response.Write("[color=Red]<option value=?<strong>bigid="&urlid&"&smaid="&rs_smallclass("id")&" ") '列出该分类下所有商品
if CStr(rs_smallclass("id"))=smaid then
Response.Write(" selected=selected")
end if
Response.Write(">"&rs_smallclass("name")&"</option>")
rs_smallclass.movenext
loop
End if
Response.Write("</sel>")
%></td>
</tr>
</table>
</form>
< %
rs_bigclass.close()
rs_smallclass.close()
Set conn=Nothing
%>
怎么样?比起复杂的那些二级联动代码是不是超级简单?诚然,两者也有不同,但我认为唯一的不是就是这个需要刷新页面(不过这恰恰是我所必需的)[sleepy][rolleyes]哈哈。。。
1 条评论 Trackback Url:http://ichov.com/web/url-submenus.html/trackback



仔细想想,上面的方法好像已经不能叫联动了,呵呵。。。
不可避免,后来还是不得不用到了二级联动菜单。
因为在添加内容时,如果用 URL 转址很不好,除非在一开始就选择好目录、分类,不然的话,如果填了内容,再选择是在转址后,之前填的内容就全洗白了。