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

Source: Internet
Author: User

Then let's start JSP + JavaScript to create a level-2 cascade drop-down menu:

Create a variable definition for the JSP JavaScript level-2 cascade drop-down menu:

◆ Class (first-level column information ):

◆ ClassId (automatic ID)

◆ ClassName (topic name)

◆ Nclass (second-level column information)

◆ NclassId (automatic ID)

◆ NclassName (topic name)

◆ ParentId (first-level column id, associated with classId in the class table)

 
 
  1. ﹤%@ page contentType=text/html; charset=GB2312 language=java errorPage=../error.jsp %﹥  
  2. ﹤%@ include file=../conn.jsp%﹥  
  3. ﹤%@ include file=../ds.jsp%﹥  
  4. ﹤%@ taglib uri=http://java.sun.com/jsp/jstl/sql divfix=sql %﹥  
  5. ﹤%request.setCharacterEncoding(gb2312); %﹥ 

Create a cascading menu for JSP + JavaScript level-2 cascading drop-down menus

 
 
  1. <HTML> <HEAD>
  2. <META.Http-equiv=Content-TypeContent=Text/Html;Charset=Gb2312>
  3.  
  4. <TITLE> cascade menu </TITLE>
  5. <LINKRel=Stylesheet Type=Text/CssHref=Style. Css>
  6. </HEAD>

◆ Obtain the second-level column information from the database

 
 
  1. ﹤%String sql=select * from Nclass order by NclassId asc;  
  2. ResultSet rs=stmt.executeQuery(sql);  
  3. %﹥  


◆ Save the second-level column information to the array subcat

 
 
  1. ﹤script. type=text/javascript﹥  
  2. var onecount;  
  3. onecount=0;  
  4. subcat = new Array();  
  5.         ﹤%  
  6.         int count = 0;  
  7.         while(rs.next()){  
  8.         %﹥  
  9. subcat[﹤%=count%﹥] = new Array(﹤%=rs.getString(NclassName)%﹥,  
  10. ﹤%=rs.getString(NclassId)%﹥,﹤%=rs.getString(parentId)%﹥);  
  11.         ﹤%  
  12.         count++;  
  13.         }  
  14.         rs.close();  
  15.         %﹥  
  16. onecount=﹤%=count%﹥;  

◆ Determines the function displayed by the Select statement.

 
 
  1.  
  2. function changelocation(locationid)  
  3.     {  
  4.     document.myform.NclassId.length = 0;  
  5.  
  6.     var locationidlocationid=locationid;  
  7.     var i;  
  8.     for (i=0;i ﹤ onecount; i++)  
  9.         {  
  10.             if (subcat[i][2] == locationid)  
  11.             {  
  12.                 document.myform.NclassId.options[document.myform.NclassId.length] = new  
  13. Option(subcat[i][0], subcat[i][1]);  
  14.             }         
  15.         }  
  16.          
  17.     }     
  18. ﹤/script﹥  

◆ JSP JavaScript level-2 cascade menu Creation

 
 
  1. <FORM.Method=POST Name=Myform.Action=Adminsave. Jsp?Action=Add>
  2. <TABLE>
  3. <TR>
  4. <TD> Level 1 Classification </TD>
  5. <TD>
  6. <SELECTName=ClassId NChange=Changelocation(Document. myform. classId. options
  7. [Document. myform. classId. selectedIndex]. value)Size=1>
  8. <OPTION selected value >== select a level-1 category ==</ OPTION>
  9. <SQL: queryVar=Query DataSource=﹥>
  10. SELECT * FROM class
  11. </SQL: query>
  12. <C: forEachVar=Row Items=﹥>
  13. <OptionValue=$ >$ </Option>
  14. </C: forEach>
  15. </Select>
  16. </TD>
  17. <TD> Select secondary category </TD>
  18. <TD>
  19. <SELECTName=NclassId>
  20. <OPTION selected value >== select Level 2 Category ==</ OPTION>
  21. </SELECT>
  22. </TD>
  23. </TR>
  24. </TABLE>
  25. </FORM>
  26. </BODY>
  27. </HTML>
  28. <%@ PageContentType=Text/Html;Charset=GB2312 Language=Java ErrorPage= ../Error. jsp %>
  29. <%@ IncludeFile= ../Conn. jsp %>
  30. <%@ IncludeFile= ../Ds. jsp %>
  31. <%@ TaglibUri=Http: // Java.sun.com/jsp/jstl/ SQLDivfix=SQL%>
  32. <% Request. setCharacterEncoding (gb2312); %>
  33. <HTML> <HEAD>
  34. <META.Http-equiv=Content-TypeContent=Text/Html;Charset=Gb2312>
  35.  
  36. <TITLE> cascade menu </TITLE>
  37. <LINKRel=Stylesheet Type=Text/CssHref=Style. Css>
  38. </HEAD>
  39. <! -- Obtain the second-level column information from the database -->
  40. <%StringSQL=Select* From Nclass order by NclassId asc;
  41. ResultSetRs=Stmt. ExecuteQuery (SQL );
  42. %>
  43. <! -- Save the second-level column information to the array subcat -->
  44. <Script.Type=Text/Javascript>
  45. Var onecount;
  46. Onecount=0;
  47. Subcat=NewArray ();
  48. <%
  49. IntCount=0;
  50. While (rs. next ()){
  51. %>
  52. Subcat [<%= count %>] = new Array (<%= rs. getString (NclassName) %>,
  53. <%= Rs. getString (NclassId) % >,<%= rs. getString (parentId) %> );
  54. <%
  55. Count ++;
  56. }
  57. Rs. close ();
  58. %>
  59. Onecount= <%= Count %>;
  60. <! -- Determines the function displayed by select -->
  61. Function changelocation (locationid)
  62. {
  63. Document. myform. NclassId. length=0;
  64.  
  65. VarLocationidLocationid= Locationid;
  66. Var I;
  67. For (I=0; I <onecount; I ++)
  68. {
  69. If (subcat [I] [2] = locationid)
  70. {
  71. Document. myform. NclassId. options [document. myform. NclassId. length] = new
  72. Option (subcat [I] [0], subcat [I] [1]);
  73. }
  74. }
  75. }
  76. </Script>
  77.  
  78. <FORM.Method=POST Name=Myform.Action=Adminsave. Jsp?Action=Add>
  79. <TABLE>
  80. <TR>
  81. <TD> Level 1 Classification </TD>
  82. <TD>
  83. <SELECTName=ClassId NChange=Changelocation(Document. myform. classId. options
  84. [Document. myform. classId. selectedIndex]. value)Size=1>
  85. <OPTION selected value >== select a level-1 category ==</ OPTION>
  86. <SQL: queryVar=Query DataSource=﹥>
  87. SELECT * FROM class
  88. </SQL: query>
  89. <C: forEachVar=Row Items=﹥>
  90. <OptionValue=$ >$ </Option>
  91. </C: forEach>
  92. </Select>
  93. </TD>
  94. <TD> Select secondary category </TD>
  95. <TD>
  96. <SELECTName=NclassId>
  97. <OPTION selected value >== select Level 2 Category ==</ OPTION>
  98. </SELECT>
  99. </TD>
  100. </TR>
  101. </TABLE>
  102. </FORM>
  103. </BODY>
  104. </HTML>
  105.  

The above is how to build a level-2 cascade drop-down menu Using JSP JavaScript. Is it good after implementation? Is it helpful for JSP JavaScript development and design?

  1. A brief overview of embedded objects in JSP
  2. JSP entry-level website environment setup steps
  3. How to export Oracle Data Tables Using JSP
  4. Implement page Jump in JSP
  5. What is JSP and comparison with Servlet?

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.