Ajxa省市联动

目录:
在这里插入图片描述
reginClass.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
<script type="text/javascript">
//创建对象
function getXmlhttpRequest(){
	var xmlHttpRequest;
	if (window.XMLHttpRequest)
	{//  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
		xmlHttpRequest=new XMLHttpRequest();
	}
	else
	{  // IE6, IE5 浏览器执行代码
		xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlHttpRequest;
}
//
	var myXmlHttpRequest="";
	function getCity() {
		myXmlHttpRequest=getXmlhttpRequest();
		if(myXmlHttpRequest){
			var url="showCity.jsp";
			var data="province="+document.getElementById("province").value;
			myXmlHttpRequest.open("post",url,true);
			myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			myXmlHttpRequest.onreadystatechange=function(){
				//alert(myXmlHttpRequest.readyState+":"+myXmlHttpRequest.status);
				if(myXmlHttpRequest.readyState==4){
					//window.alert("back:"+myXmlHttpRequest.responseXML);
					var cities=myXmlHttpRequest.responseXML.getElementsByTagName("city");
					//alert(cities.length);
					document.getElementById("city").length=1;
					for(var i=0;i<cities.length;i++){
						var city=cities[i].childNodes[0].nodeValue;
						var myOption=document.createElement("option");
						myOption.innerHTML=city;
						
						document.getElementById("city").appendChild(myOption);
						//alert("yes");
					}
				}
			}
			myXmlHttpRequest.send(data);
			
		}
	}
	
 
var myXmlHttpRequest2="";
function getTown() {
	myXmlHttpRequest2=getXmlhttpRequest();
	if(myXmlHttpRequest){
		var url="showCounty.jsp";
		var data="city="+document.getElementById("city").value;
		myXmlHttpRequest2.open("post",url,true);
		myXmlHttpRequest2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		myXmlHttpRequest2.onreadystatechange=function(){
			//alert(myXmlHttpRequest2.readyState+":"+myXmlHttpRequest2.status);
			if(myXmlHttpRequest2.readyState==4){
				//window.alert("back:"+myXmlHttpRequest2.responseXML);
				var towns=myXmlHttpRequest2.responseXML.getElementsByTagName("town");
				//alert(cities.length);
				document.getElementById("town").length=1;
				for(var i=0;i<towns.length;i++){
					var town=towns[i].childNodes[0].nodeValue;
					var myOption=document.createElement("option");
					myOption.innerHTML=town;
					
					document.getElementById("town").appendChild(myOption);
					//alert("yes");
				}
			}
		}
		myXmlHttpRequest2.send(data);
		
	
	}
}
</script>
</head>
<body>
<select id="province" onchange="getCity()">
<option>--省--</option>
<option value="山西">山西省</option>
<option value="湖北">湖北省</option>
</select>
<select id="city" onchange="getTown()"><option>--市--</option></select>
<select id="town"><option>--县--</option></select>
</body>
</html>

showCity.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%   response.setContentType("text/xml;charset=utf-8");
    String province=request.getParameter("province");
    //System.out.println(province);
    System.out.println(new String(province.getBytes("iso-8859-1"),"utf-8"));
    province=new String(province.getBytes("iso-8859-1"),"utf-8");
   if(province.equals("山西")){
   out.println("<province><city>太原</city><city>晋中</city><city>临汾</city></province>");
   //System.out.print(province.equals("山西"));
    }
    else if(province.equals("湖北")){
    	out.println("<province><city>武汉</city><city>咸宁</city><city>黄石</city></province>");
    }
  %>

showCounty.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%   response.setContentType("text/xml;charset=utf-8");
    String city=request.getParameter("city");
    //System.out.println(province);
    System.out.println(new String(city.getBytes("iso-8859-1"),"utf-8"));
   city=new String(city.getBytes("iso-8859-1"),"utf-8");
   if(city.equals("太原")){
   out.println("<city><town>迎泽</town><town>小店</town><town>府东</town></city>");
   //System.out.print(province.equals("山西"));
    }
    else if(city.equals("晋中")){
    	out.println("<city><town>平遥</town><town>太谷</town><town><祁县/town></city>");
    }
    else if(city.equals("临汾")){
    	out.println("<city><town>尧都区</town><town>洪洞县</town><town>襄汾县</town></city>");
    }
    else if(city.equals("武汉")){
    	out.println("<city><town>江岸区</town><town>江汉区</town><town>武昌区</town></city>");
    }
    else if(city.equals("咸宁")){
    	out.println("<city><town>咸安区</town><town>赤壁市</town><town>嘉鱼县</town></city>");
    }
    else if(city.equals("黄石")){
    	out.println("<city><town>阳新县</town><town>阳新县</town><town>阳新县</town></city>");
    }
    %>

结果
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值