论坛首页 Web前端技术论坛

AJAX实现级联菜单示例

浏览 5183 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2007-11-30  
J +JavaScript打造二级级联下拉菜单:

cla (一级栏目信息):cla Id(自动编号),cla ame(栏目名称), Ncla (二级栏目信息), Ncla Id(自动编号),Ncla ame(栏目名称),parentId(一级栏目id,与cla 表中的cla Id关联)

<%@ page contentType=text/html; charset=GB2312 language=java errorPage=../error.j  %>
<%@ include file=../co .j %>
<%@ include file=../ds.j %>
<%@ taglib uri=http://java.sun.com/j /jstl/sql divfix=sql %>
<%request.setCharacterEncoding(gb2312); %>
<HTML> lt;HEAD>
<META http-equiv=Content-Type content=text/html; charset=gb2312>
<TITLE>级联菜单</TITLE>
<LINK rel=stylesheet type=text/c  href=style.c gt;
</HEAD>
<!--从数据库中得到二级栏目信息-->
<%String sql=select * from Ncla  order by Ncla Id asc;
ResultSet rs=stmt.executeQuery(sql);
%>
<!--将二级栏目信息保存到数组subcat中-->
< cript type=text/javascript>
var onecount;
onecount=0;
subcat = new Array();
  <%
  int count = 0;
  while(rs.next()){
  %>
subcat[<%=count%>] = new Array(<%=rs.getString(Ncla ame)%>,
<%=rs.getString(Ncla Id)%>,<%=rs.getString(parentId)%>);
  <%
  count++;
  }
  rs.close();
  %>
onecount=<%=count%> 
<!--决定select显示的函数-->
function changelocation(locationid)
  {
  document.myform.Ncla Id.length = 0;

  var locationid=locationid;
  var i;
  for (i=0;i < onecount; i++)
  {
  if (subcat[i][2] == locationid)
  {
  document.myform.Ncla Id.optio [document.myform.Ncla Id.length] = new
Option(subcat[i][0], subcat[i][1]);
  } 
  }
  } 
</script>
<FORM method=POST name=myform action=admi ave.j ?action=add>
  <TABLE>
 
  <TR>
  <TD>一级分类</TD>
  <TD> 
  < ELECT name=cla Id onChange=changelocation(document.myform.cla Id.optio
[document.myform.cla Id.selectedIndex].value) size=1>
  <OPTION selected value>==请选一级分类==</OPTIO gt;
  < ql:query var=query dataSource=$>
  SELECT * FROM cla 
  </sql:query>
<c:forEach var=row items=$>
lt;option value=$>$</optio gt;
</c:forEach>
</select>
  </TD>
  <TD>选择二级分类</TD>
  <TD>
  < ELECT name=Ncla Id>
  <OPTION selected value>==请选二级分类==</OPTIO gt;
  </SELECT>
  </TD>
  </TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<%@ page contentType=text/html; charset=GB2312 language=java errorPage=../error.j  %>
<%@ include file=../co .j %>
<%@ include file=../ds.j %>
<%@ taglib uri=http://java.sun.com/j /jstl/sql divfix=sql %>
<%request.setCharacterEncoding(gb2312); %>
<HTML> lt;HEAD>
<META http-equiv=Content-Type content=text/html; charset=gb2312>
<TITLE>级联菜单</TITLE>
<LINK rel=stylesheet type=text/c  href=style.c gt;
</HEAD>
<!--从数据库中得到二级栏目信息-->
<%String sql=select * from Ncla  order by Ncla Id asc;
ResultSet rs=stmt.executeQuery(sql);
%>
<!--将二级栏目信息保存到数组subcat中-->
< cript type=text/javascript>
var onecount;
onecount=0;
subcat = new Array();
  <%
  int count = 0;
  while(rs.next()){
  %>
subcat[<%=count%>] = new Array(<%=rs.getString(Ncla ame)%>,
<%=rs.getString(Ncla Id)%>,<%=rs.getString(parentId)%>);
  <%
  count++;
  }
  rs.close();
  %>
onecount=<%=count%> 
<!--决定select显示的函数-->
function changelocation(locationid)
  {
  document.myform.Ncla Id.length = 0;
  var locationid=locationid;
  var i;
  for (i=0;i < onecount; i++)
  {
  if (subcat[i][2] == locationid)
  {
  document.myform.Ncla Id.optio [document.myform.Ncla Id.length] = new
Option(subcat[i][0], subcat[i][1]);
  } 
  }
  } 
</script>
<FORM method=POST name=myform action=admi ave.j ?action=add>
  <TABLE>
  <TR>
  <TD>一级分类</TD>
  <TD> 
  < ELECT name=cla Id onChange=changelocation(document.myform.cla Id.optio
[document.myform.cla Id.selectedIndex].value) size=1>
  <OPTION selected value>==请选一级分类==</OPTIO gt;
  < ql:query var=query dataSource=$>
  SELECT * FROM cla 
  </sql:query>
<c:forEach var=row items=$>
lt;option value=$>$</optio gt;
</c:forEach>
</select>
  </TD>
  <TD>选择二级分类</TD>
  <TD>
  < ELECT name=Ncla Id>
  <OPTION selected value>==请选二级分类==</OPTIO gt;
  </SELECT>
  </TD>
  </TR>
</TABLE>
</FORM>
</BODY>
</HTML>
[b]
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics