AJAX省市區(qū)三級(jí)聯(lián)動(dòng)下拉菜單(java版)
此小程序的功能主要是采用異步請(qǐng)求方式從數(shù)據(jù)庫中調(diào)取省市區(qū)信息顯示到下拉列表:
代碼如下:
建立數(shù)據(jù)庫中的代碼和一些配置文件信息就省略了,主要有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("
");
//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();
}
}
}
}
主要的功能代碼都在上面了,如有不懂的請(qǐng)聯(lián)系QQ:1037139984,祝大家都有好的發(fā)展,共同學(xué)習(xí),共同成長(zhǎng)。
相關(guān)文章
java使用BeanUtils.copyProperties踩坑經(jīng)歷
最近在做個(gè)項(xiàng)目,踩了個(gè)坑特此記錄一下,本文主要介紹了使用BeanUtils.copyProperties踩坑經(jīng)歷,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
詳解SpringMVC @RequestBody接收J(rèn)son對(duì)象字符串
這篇文章主要介紹了詳解SpringMVC @RequestBody接收J(rèn)son對(duì)象字符串,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
SpringBoot項(xiàng)目中的多數(shù)據(jù)源支持的方法
本篇文章主要介紹了SpringBoot項(xiàng)目中的多數(shù)據(jù)源支持的方法,主要介紹在SpringBoot項(xiàng)目中利用SpringDataJpa技術(shù)如何支持多個(gè)數(shù)據(jù)庫的數(shù)據(jù)源,有興趣的可以了解一下2017-10-10
mybatis-plus插入一條數(shù)據(jù),獲取插入數(shù)據(jù)自動(dòng)生成的主鍵問題
這篇文章主要介紹了mybatis-plus插入一條數(shù)據(jù),獲取插入數(shù)據(jù)自動(dòng)生成的主鍵問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

