Create a level-2 cascade drop-down menu Using JSP + JavaScript

Source: Internet
Author: User

JSP + JavaScript creates a level-2 cascade drop-down menu:


Class (first-level column information): classId (automatic number), className (column name), Nclass (second-level column information), NclassId (automatic number), NclassName (column name ), parentId (first-level column id, associated with classId in the class table)

 

Reference content is as follows:

<% @ Page contentType = text/html; charset = GB2312 language = java errorPage =.../error. jsp %>
<% @ Include file = ../conn. jsp %>
<% @ Include file = ../ds. jsp %>
& Lt; % @ taglib uri = http://java.sun.com/jsp/jstl/ SQL divfix = SQL % & gt;
<% Request. setCharacterEncoding (gb2312); %>
<HTML> <HEAD>
<META http-equiv = Content-Type content = text/html; charset = gb2312>

<TITLE> cascade menu </TITLE>
<LINK rel = stylesheet type = text/css href?style.css>
</HEAD>
<! -- Obtain the second-level column information from the database -->
<% String SQL = select * from Nclass order by NclassId asc;
ResultSet rs1_stmt.exe cuteQuery (SQL );
%>
<! -- Save the second-level column information to the array 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 %>;
<! -- Determines the function displayed by 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> Level 1 Classification </TD>
<TD>
<SELECT name = classId onChange = changelocation (document. myform. classId. options
[Document. myform. classId. selectedIndex]. value) size = 1>
<OPTION selected value >== select a level-1 category ==</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 secondary category </TD>
<TD>
<SELECT name = NclassId>
<OPTION selected value >== select Level 2 Category ==</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 %>
& Lt; % @ taglib uri = http://java.sun.com/jsp/jstl/ SQL divfix = SQL % & gt;
<% Request. setCharacterEncoding (gb2312); %>
<HTML> <HEAD>
<META http-equiv = Content-Type content = text/html; charset = gb2312>

<TITLE> cascade menu </TITLE>
<LINK rel = stylesheet type = text/css href?style.css>
</HEAD>
<! -- Obtain the second-level column information from the database -->
<% String SQL = select * from Nclass order by NclassId asc;
ResultSet rs1_stmt.exe cuteQuery (SQL );
%>
<! -- Save the second-level column information to the array 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 %>;
<! -- Determines the function displayed by 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> Level 1 Classification </TD>
<TD>
<SELECT name = classId onChange = changelocation (document. myform. classId. options
[Document. myform. classId. selectedIndex]. value) size = 1>
<OPTION selected value >== select a level-1 category ==</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 secondary category </TD>
<TD>
<SELECT name = NclassId>
<OPTION selected value >== select Level 2 Category ==</OPTION>
</SELECT>
</TD>
</TR>
</TABLE>

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

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.