Create a two Cascade pull-down menu with Jsp+javascript

Source: Internet
Author: User
Tags add foreach array count include sql string stmt

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, Associated with the ClassID in the class table)

<%@ 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> 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 onchange=changelocation (document.myform.classId.options
[Document.myform.classid.selectedindex].value) size=1>
<option selected value>== Please select one level of classification ==</option>
<sql:query Var=query datasource=$>
SELECT * FROM class
</sql:query>
<c:foreach Var=row items=$>
<option value=$>$</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>
<%@ 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> 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 onchange=changelocation (document.myform.classId.options
[Document.myform.classid.selectedindex].value) size=1>
<option selected value>== Please select one level of classification ==</option>
<sql:query Var=query datasource=$>
SELECT * FROM class
</sql:query>
<c:foreach Var=row items=$>
<option value=$>$</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

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.