目录:
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>");
}
%>
结果