欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AJAX省市区三级联动下拉菜单(java版)

程序员文章站 2024-03-08 10:54:58
此小程序的功能主要是采用异步请求方式从数据库中调取省市区信息显示到下拉列表: 代码如下: 建立数据库中的代码和一些配置文件信息就省略了,主要有javascript中的代...

此小程序的功能主要是采用异步请求方式从数据库中调取省市区信息显示到下拉列表:

代码如下:

建立数据库中的代码和一些配置文件信息就省略了,主要有javascript中的代码为:

$(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);
}

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= servletactioncontext.getresponse();
httpservletrequest request = servletactioncontext.getrequest();
//resp.setcontenttype("xml");
resp.setcontenttype("text/html");
resp.setcharacterencoding("utf-8");
try {
printwriter out = resp.getwriter();
out.print("AJAX省市区三级联动下拉菜单(java版)");
//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("AJAX省市区三级联动下拉菜单(java版)");
//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("AJAX省市区三级联动下拉菜单(java版)");
} catch (ioexception e) {
e.printstacktrace();
}
}
}
}

主要的功能代码都在上面了,如有不懂的请联系qq:1037139984,祝大家都有好的发展,共同学习,共同成长。