java省市区三级联动_AJAX省市区三级联动下拉菜单(java
版)
此⼩程序的功能主要是采⽤异步请求⽅式从数据库中调取省市区信息显⽰到下拉列表:
代码如下:
建⽴数据库中的代码和⼀些配置⽂件信息就省略了,主要有JavaScript中的代码为:
$(document).ready(function(){
$.get("getProvince.do", function(result){
$("#showp").html(result);
});
})
var xmlhttp;
function mysend(str){
$(document).ready(function(){
$("#show2").html("");
})
var show = ElementByIdx_x_x_x_x_x_x_x_x_x("show");
show.innerHTML = "";
var province = ElementByIdx_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");
}
adyState==4 && xmlhttp.status==200){
show.innerHTML = sponseText;
}
}
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");
}
adyState==4 && xmlhttp.status==200){
var show2 = ElementByIdx_x_x_x_x_x_x_x_x_x("show2"); show2.innerHTML = sponseText;
}
}
var ss = encodeURIComponent(str);
xmlhttp.open("GET","getArea.do?cityid="+ss,true);
xmlhttp.send(null);
}
html页⾯中的代码为:
所在地
action中的代码为:
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 getProvince(){
List provinceList = as.getAreasPrvinceList();
HttpServletResponse resp= Response(); HttpServletRequest request = Request();
//resp.setContentType("xml");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
java调用js的ajaxtry {
PrintWriter out = Writer();
out.print("
"); //out.print("shanghai");
} catch (IOException e) {
e.printStackTrace();
}
}
public void getCity(){
List cityList = as.getAreasCityList(provinceid);
HttpServletResponse resp= Response();
//resp.setContentType("xml");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
try {
PrintWriter out = Writer();
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= Response();
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
try {
PrintWriter out = Writer();
out.print("
");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
主要的功能代码都在上⾯了,如有不懂的请联系QQ:1037139984,祝⼤家都有好的发展,共同学习,共同成长。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。