Ajax asynchronously requests data and performs drop-down box Interaction

Source: Internet
Author: User
Ajax asynchronously requests data and performs drop-down box Interaction

 

Bytes
  Ajax code and JS Code:

 

VaR XMLHTTP;
Function getdata (){
// Obtain the province name
VaR provincename = $ ("# select1"). Val ();
// Create an asynchronous call object
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
// Load the page to be linked
XMLHTTP. Open ("Post", "nodelist. jsp? Query = query & name = "+ encodeuri (provincename), true );
// Associate object states with events
XMLHTTP. onreadystatechange = statechange;
// Send the request
XMLHTTP. Send ();
}
Function statechange (){
$ ("# Select2"). Empty (); // clear the drop-down box option
// Determine whether the asynchronous call is complete
If (XMLHTTP. readystate = 4 ){
// Determine whether the code is OK
If (XMLHTTP. Status = 200 ){
If (XMLHTTP. responsetext! = "" & XMLHTTP. responsetext! = NULL ){
VaR city = "";
City = XMLHTTP. responsetext;
VaR select2 = Document. getelementbyidx_x ("select2 ");
VaR option = new array (); // defines an array
VaR option2 = new array (); // defines an array
VaR option3 = new array (); // defines an array
Option3 = city. Split ("*"); // delimiter
Option = option3 [0]. Split (",");
Option2 = option3 [1]. Split (",");
For (VAR I = 0; I
VaR opt = Document. createelement_x ("option ");
Opt. value = option2 [I]; // you can specify the value of an option.
Opt. innerhtml = option [I]; // sets the value displayed to the user.
Select2.appendchild (OPT );
}
}

}
}
}

Java call code:

List r = NULL;
Regionservice = new regionservice ();
String query = request. getparameter ("query ");
If ("query". Equals (query )){
String arr = "";

String arr2 = "";
String name = new string (request. getparameter ("name"). getbytes ("ISO-8859-1"), "UTF-8"); // request encoding conversion
If (! "". Equals (name) & name! = NULL ){
R = regionservice. getregionbyname (name );
For (INT I = 0; I
Arr + = R. Get (I). getcityname () + ",";

Arr2 + = R. Get (I). GETID () + ",";

}
}
Out. println (ARR + "*" + arr2); // important
Return; // return, if the same page is accessed
}

JSP code:

Select province:
Id = "select1" onchange = "getdata ();">
Select Province

 

Related Article

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.