Jsp+javascript Create two Cascade pull-down menu

Source: Internet
Author: User
Tags foreach array count include sql query
javascript|js| Menu | dropdown

Database Requirements Analysis:
Class (First-level column information): classId (Automatic numbering), className (column name)
NClass (Level two column information): NCLASSID (Automatic numbering), nclassname (column name), ParentID (First level column ID, and the class table in the ClassID Association)

<%@ 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" prefix= "SQL"%>
<%request.setcharacterencoding ("gb2312"); %>
<HTML><HEAD>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<TITLE> cascading Menu </TITLE>
<link rel= "stylesheet" type= "Text/css" href= "Style.css" >
</HEAD>
<!--get two-level column information from the database-->
<%string sql= "SELECT * from NClass ORDER by nclassid ASC";
ResultSet rs=stmt.executequery (SQL);
%>
<!--to save the level two column information to the array subcat-->
<script type= "Text/javascript" >
var Onecount;
onecount=0;
subcat = new Array ();
<%
int count = 0;
while (Rs.next ()) {
%>
SUBCAT[&LT;%=COUNT%&GT] = new Array ("<%=rs.getstring" ("Nclassname")%> "," <%=rs.getstring ("Nclassid")%> "," <%=rs.getstring ("ParentID")%> ");
<%
count++;
}
Rs.close ();
%>
onecount=<%=count%>;
<!--determine the function--> the Select display
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> First Class </TD>
<TD>
<select name= "ClassId" size= "1" >
<option selected value>== Please select one level of classification ==</option>
<sql:query var= "Query" datasource= "${bookdev}" >
SELECT * FROM class
</sql:query>
<c:foreach var= "Row" items= "${query.rows}" >
<option value= "${row.classid}" >${row.className}</option>
</c:forEach>
</select>
</TD>
<TD> Select Class Two </TD>
<TD>
<select name= "Nclassid" >
<option Selected value>== Please select Class Two classification ==</option>
</SELECT>
</TD>
</TR>
</TABLE>

</FORM>
</BODY>
</HTML>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.