Ajax asynchronously requests data and performs drop-down box Interaction
Bytes
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