那么我们开始JSP+JavaScript打造二级级联下拉菜单:
成都创新互联专注于网站建设|成都网站改版|优化|托管以及网络推广,积累了大量的网站设计与制作经验,为许多企业提供了网站定制设计服务,案例作品覆盖卫生间隔断等行业。能根据企业所处的行业与销售的产品,结合品牌形象的塑造,量身设计品质网站。
打造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); %﹥
打造JSP+JavaScript二级级联下拉菜单之级联菜单制作
- ﹤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 locationidlocationid=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﹥
◆JSP JavaScript二级级联菜单的制作
- ﹤FORM. method=POST name=myform. action=adminsave.jsp?action=add﹥
- ﹤TABLE﹥
- ﹤TR﹥
- ﹤TD﹥一级分类﹤/TD﹥
- ﹤TD﹥
- ﹤SELECT name=classId nChange=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 locationidlocationid=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 nChange=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﹥
以上就是使用JSP JavaScript如何构建二级级联下拉菜单的方法,具体实现之后是不是很好呢?对JSP JavaScript开发设计是不是有帮助呢?
新闻标题:打造JSP+JavaScript二级级联下拉菜单
分享网址:http://www.shufengxianlan.com/qtweb/news33/6033.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联