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>
|