The main function of this applet is to use asynchronous request to transfer the city and province information from the database to display to the Drop-down list:
The code is as follows:
The code in the database and some configuration file information are omitted, and the main JavaScript code is:
$ (document). Ready (function () {$.get ("getprovince.do", function (Result) {$ (' #showp '). HTML (result);});}) var xmlhttp;
function Mysend (str) {$ (document). Ready (function () {$ ("#show2"). HTML ("");})
var show = Document.getelementbyidx_x_x_x_x_x_x_x_x_x ("show");
show.innerhtml = "";
var province = document.getelementbyidx_x_x_x_x_x_x_x_x_x ("province"). Value; if (province!=0) {if (window). XMLHttpRequest) {xmlhttp = new XMLHttpRequest ();} else{xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange=function () {if (
Xmlhttp.readystate==4 && xmlhttp.status==200) {show.innerhtml = Xmlhttp.responsetext;}}
var ss = encodeURIComponent (str);
Xmlhttp.open ("Get", "getcity.do?provinceid=" +ss,true);
Xmlhttp.send (NULL); }} function Myarea (str) {if window. XMLHttpRequest) {xmlhttp = new XMLHttpRequest ();} else{xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange=function () {if ( Xmlhttp.readystate==4 && xmlhttp.status==200) {var show2 = DocumenT.getelementbyidx_x_x_x_x_x_x_x_x_x ("Show2");
show2.innerhtml = Xmlhttp.responsetext;
} var ss = encodeURIComponent (str);
Xmlhttp.open ("Get", "getarea.do?cityid=" +ss,true);
Xmlhttp.send (NULL); }
The code in the HTML page is:
Location
The code in the action is:
Package mobi.zhangsheng.jiejia.action;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.List;
Import Javax.annotation.Resource;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.struts2.ServletActionContext;
Import Org.springframework.stereotype.Controller;
Import Mobi.zhangsheng.jiejia.domain.Areas;
Import Mobi.zhangsheng.jiejia.service.AgentsService;
Import Mobi.zhangsheng.jiejia.service.AreasService; @Controller public class Provinceaction {private int provinceid; private int cityid; @Resource private areasservice as; @
Resource private Agentsservice AGS; public int Getprovinceid () {return provinceid.} public void Setprovinceid (int provinceid) {This.provinceid = Provinceid
; public int Getcityid () {return Cityid.} public void Setcityid (int cityid) {This.cityid = Cityid;} public void Getpro
Vince () {List provincelist = as.getareasprvincelist (); HttpServletResponse resp= SERVLETACTIONCONTEXT.GEtresponse ();
HttpServletRequest request = Servletactioncontext.getrequest ();
Resp.setcontenttype ("xml");
Resp.setcontenttype ("text/html");
Resp.setcharacterencoding ("Utf-8");
try {printwriter out = Resp.getwriter (); Out.print ("");
Out.print ("Shanghai");
catch (IOException e) {e.printstacktrace ();}}
public void Getcity () {List citylist = as.getareascitylist (Provinceid);
HttpServletResponse resp= servletactioncontext.getresponse ();
Resp.setcontenttype ("xml");
Resp.setcontenttype ("text/html");
Resp.setcharacterencoding ("Utf-8");
try {printwriter out = Resp.getwriter (); Out.print ("");
Out.print ("Shanghai");
catch (IOException e) {e.printstacktrace ();}} public void Getarea () {List arealist = as.getareascitylist (Cityid); if (Arealist.size () ==0) {}else{HttpServletResponse
resp= Servletactioncontext.getresponse ();
Resp.setcontenttype ("text/html");
Resp.setcharacterencoding ("Utf-8"); try {printwriter out = Resp.getwriter (); Out.print ("");} catch (IOException e) {e.printsTacktrace (); }
}
}
}
The main functional code is above, if you do not understand please contact qq:1037139984, I wish you all have a good development, learn together and grow together.