jquery dynamic load Select dropdown Box Sample code _jquery

Source: Internet
Author: User
Tags stringbuffer
Title, directly on the code, Combat learning.
Copy Code code as follows:

<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>
<body onload= "init ()" >
Dropdown box <select name= "Makemodule" id= "Makemodule" style= ' width:130px ' onchange= ' makemoduleselected ' () ' >
<option>-------</option>
</select></body>

When the above HTML is loaded, because the onload attribute is set in the body tag, its corresponding JavaScript function will run, and finally to function makemoduleselect (), take a look at it:

URL properties, similar to the AJAX jump URL, here I use the same path of the JSP page (indexstatisticsaction_getselect.jsp), the following will be shown;
The Data property, which is taken as the requested parameter, is fetched by the request;
The Success property, which represents the code that will execute after the AJAX request for this jquery is successfully returned, where $ ("#makemodule") refers to the Drop-down box.

The following is the AJAX request for the 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 needs of the business logic code out.
Copy Code 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= '? modulename_e N? ' GROUP by 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")) {
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.executequery (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, which are the display and truth values of the Select Drop-down box, and the Set <option> label postback is OK.

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.