您现在的位置: 天下网吧 >> 网吧天地 >> 天下码农 >> 架构师 >> 正文

用JSP+JavaScript打造二级级联下拉菜单

2008-4-8赛迪网佚名

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="http://java.ccidnet.com/art/3539/20080623/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="http://java.ccidnet.com/art/3539/20080623/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>

欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛https://bbs.txwb.com

关注天下网吧微信,了解网吧网咖经营管理,安装维护:


本文来源:赛迪网 作者:佚名

声明
本文来源地址:0
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系邮箱:support@txwb.com.,本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧 网吧天下
  • 本周热门
  • 本月热门
  • 阅读排行