jquery dynamic load Select dropdown Box Sample code

Source: Internet
Author: User
Tags stringbuffer

The

  Dynamic load Select Drop-down box is implemented in a number of ways, and in the following articles, let's show you how jquery is implemented

Title, directly on the code, Combat learning.   Code as follows: <head><title>jquery implementation dynamic load Select Drop-down options </title>  <script type= "Text/javascript" >  function init () {  makemoduleselect (); } //Load template dropdown box options   function Makemoduleselect () {   $.ajax ({  URL: ' indexstatisticsaction_getselect.jsp ',  data: {page: ' clientindexstatistics.jsp ', Method: ' Get_modtitlecode '},  success:function (Result) {  $ ("#makemodule"). Append (Result); } } ); }</script>  </head>  <body onload= "init ()" >  dropdown box <select name= " Makemodule "id=" Makemodule "style= ' width:130px ' onchange= ' makemoduleselected () ' >  <option>------- </option>  </select></body>    When HTML is loaded, the onload attribute is set in the body tag, Then its corresponding JavaScript function will run, and finally to function Makemoduleselect (), and then take a look at this function:    URL properties, similar to the AJAX jump URL, Here I use the same path to the JSP page (indexstatisticsaction_getselect.jsp), the following will show the;  data property, will be the requested parameters, from request to get;  success Properties TableThis jquery Ajax request will be executed after successful return of the code, where the $ ("#makemodule") refers to the Drop-down box.     Below is the Ajax requested URL of the JSP, where the deletion of the JDBC-related packages, the introduction of their own, JDBC is not much to say, according to the need to put the business logic code out.   Code as follows: <%@ page import= "java.util.*"%>  <%@ page import= "java.sql.ResultSet"%>  <%@ page Import= "Java.io.PrintWriter"%>  <%  String userId = (String) session.getattribute ("UserId");  String method = Request.getparameter (' method ');  string frompage = Request.getparameter ("page");  string SQL1 = "Select Modtitlename,modtitlecode from makemodule where UserID = '? userid? ' and modulename_en= '? Modtitlename ";  System.out.println ("---getting select_option from: "+frompage+"----"+ new Date ());   / /Get Template Options   if (Method.equals ("Get_modtitlecode")) {  String sql = Sql1.replace ("? userId?", UserId);  if ( Frompage.equals ("acindexstatistics.jsp")) {  sql = Sql.replace ("? modulename_en?", "Acsta"); }else if ( Frompage.equals ("apindexstatistics.jsp")) {&nbsP sql = Sql.replace ("? modulename_en?", "Apsta"); }else if (frompage.equals ("clientindexstatistics.jsp")) {  sql = Sql.replace ("? modulename_en?", "Terminalsta"); }  System.out.println (SQL);  StringBuffer Rsoption = new StringBuffer ();  db db = new db ();  try {  db.preparequery ();  ResultSet rs = db.execut Equery (SQL);  while (Rs!=null && rs.next ()) {  rsoption.append ("<option value= '" +rs.getstring (" Modtitlecode ") +" ' > "+STRINGOPERATOR.ISO2GB (rs.getstring (" Modtitlename ")) +" </option> "); }  Rs.close (); } catch (Exception e) {  e.printstacktrace (); } finally {  Db.endquery (); }  PrintWriter pout = Response.getwriter ();  pout.write (rsoption.tostring ());  Pout.flush ();  pout.close (); } %>    The SQL statement above will take out two values, respectively, for the display and truth values of the Select Drop-down box, and the set <option> tag postback is OK.  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

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.