用JSP+JavaScript打造二级级联下拉菜单
来源: 阅读:1521 次 日期:2015-01-27 14:41:02
温馨提示: 小编为您整理了“用JSP+JavaScript打造二级级联下拉菜单”,方便广大网友查阅!

class(一级栏目信息):classId(自动编号),className(栏目名称), Nclass(二级栏目信息), NclassId(自动编号),NclassName(栏目名称),parentId(一级栏目id,与class表中的classId关联)<%@ page contentType=text/html; charset=GB2312 language=java errorPage=../error.jsp %>

<%@ include file=../conn.jsp%>

<%@ include file=../ds.jsp%>

<%@ taglib uri=http://java.sun.com/jsp/jstl/sql divfix=sql %>

<%request.setCharacterEncoding(gb2312); %>

<HTML><HEAD>

<META http-equiv=Content-Type content=text/html; charset=gb2312>

<TITLE>级联菜单</TITLE>

<LINK rel=stylesheet type=text/css href=style.css>

</HEAD>

<!--从数据库中得到二级栏目信息-->

<%String sql=select * from Nclass order by NclassId asc;

ResultSet rs=stmt.executeQuery(sql);

%>

<!--将二级栏目信息保存到数组subcat中-->

<script type=text/javascript>

var onecount;

onecount=0;

subcat = new Array();

<%

int count = 0;

while(rs.next()){

%>

subcat[<%=count%>] = new Array(<%=rs.getString(NclassName)%>,

<%=rs.getString(NclassId)%>,<%=rs.getString(parentId)%>);

<%

count++;

}

rs.close();

%>

onecount=<%=count%>;

<!--决定select显示的函数-->

function changelocation(locationid)

{

document.myform.NclassId.length = 0;

var locationid=locationid;

var i;

for (i=0;i < onecount; i++)

{

if (subcat[i][2] == locationid)

{

document.myform.NclassId.options[document.myform.NclassId.length] = new

Option(subcat[i][0], subcat[i][1]);

}

}

}

</script>

<FORM method=POST name=myform action=adminsave.jsp?action=add>

<TABLE>

<TR>

<TD>一级分类</TD>

<TD> 

<SELECT name=classId onChange=changelocation(document.myform.classId.options

[document.myform.classId.selectedIndex].value) size=1>

<OPTION selected value>==请选一级分类==</OPTION>

<sql:query var=query dataSource=$>

SELECT * FROM class

</sql:query>

<c:forEach var=row items=$>

<option value=$>$</option>

</c:forEach>

</select>

</TD>

<TD>选择二级分类</TD>

<TD>

<SELECT name=NclassId>

<OPTION selected value>==请选二级分类==</OPTION>

</SELECT>

</TD>

</TR>

</TABLE>

</FORM>

</BODY>

</HTML>

<%@ page contentType=text/html; charset=GB2312 language=java errorPage=../error.jsp %>

<%@ include file=../conn.jsp%>

<%@ include file=../ds.jsp%>

<%@ taglib uri=http://java.sun.com/jsp/jstl/sql divfix=sql %>

<%request.setCharacterEncoding(gb2312); %>

<HTML><HEAD>

<META http-equiv=Content-Type content=text/html; charset=gb2312>

<TITLE>级联菜单</TITLE>

<LINK rel=stylesheet type=text/css href=style.css>

</HEAD>

<!--从数据库中得到二级栏目信息-->

<%String sql=select * from Nclass order by NclassId asc;

ResultSet rs=stmt.executeQuery(sql);

%>

<!--将二级栏目信息保存到数组subcat中-->

<script type=text/javascript>

var onecount;

onecount=0;

subcat = new Array();

<%

int count = 0;

while(rs.next()){

%>

subcat[<%=count%>] = new Array(<%=rs.getString(NclassName)%>,

<%=rs.getString(NclassId)%>,<%=rs.getString(parentId)%>);

<%

count++;

}

rs.close();

%>

onecount=<%=count%>;

<!--决定select显示的函数-->

function changelocation(locationid)

{

document.myform.NclassId.length = 0;

var locationid=locationid;

var i;

for (i=0;i < onecount; i++)

{

if (subcat[i][2] == locationid)

{

document.myform.NclassId.options[document.myform.NclassId.length] = new

Option(subcat[i][0], subcat[i][1]);

}

}

}

</script>

<FORM method=POST name=myform action=adminsave.jsp?action=add>

<TABLE>

<TR>

<TD>一级分类</TD>

<TD> 

<SELECT name=classId onChange=changelocation(document.myform.classId.options

[document.myform.classId.selectedIndex].value) size=1>

<OPTION selected value>==请选一级分类==</OPTION>

<sql:query var=query dataSource=$>

SELECT * FROM class

</sql:query>

<c:forEach var=row items=$>

<option value=$>$</option>

</c:forEach>

</select>

</TD>

<TD>选择二级分类</TD>

<TD>

<SELECT name=NclassId>

<OPTION selected value>==请选二级分类==</OPTION>

</SELECT>

</TD>

</TR>

</TABLE>

</FORM>

</BODY>

</HTML>

更多信息请查看IT技术专栏

更多信息请查看脚本栏目
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2025国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00)版权所有:
云南网警报警专用图标
Baidu
map