Ajax provincial three-level linkage drop-down menu (Java version) _java

Source: Internet
Author: User

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.

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.