-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowCities.php
85 lines (68 loc) · 1.96 KB
/
showCities.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script>
//创建ajax引擎
function getXmlHttpObject(){
var xmlHttpRequest;
//不同的浏览器获取xmlhttprequest对象的方法不一样
if(window.ActiveXObject){
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest=new XMLHttpRequest();
}
return xmlHttpRequest;
}
var myXmlHttpRequest = "";
function getCities(){
myXmlHttpRequest = getXmlHttpObject();
if(myXmlHttpRequest){
var url = "/ajax/showCitiesPro.php";
var data = "Provice="+$("sheng").value;
myXmlHttpRequest.open("post",url,true); //异步方式
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//指定回调函数
myXmlHttpRequest.onreadystatechange = chuli;
//发送请求
myXmlHttpRequest.send(data);
}
}
function chuli(){
if(myXmlHttpRequest.readyState == 4){
if(myXmlHttpRequest.status == 200){
//取出服务器返回的数据
var cities = myXmlHttpRequest.responseXML.getElementsByTagName("city");
$("city").length = 0;
var myOption = document.createElement("option");
myOption.innerText = "--城市--";
$("city").appendChild(myOption);
for(var i=0;i<cities.length;i++){
var city_name = cities[i].childNodes[0].nodeValue;
var myOption = document.createElement("option");
myOption.value = city_name;
myOption.innerText = city_name;
$("city").appendChild(myOption);
}
}
}
}
//这里我们写一个函数
function $(id){
return document.getElementById(id);
}
</script>
</head>
<body>
<select id="sheng" onchange="getCities()">
<option value="">---省---</option>
<option value="zhejiang">浙江</option>
<option value="jiangsu">江苏</option>
</select>
<select id="city">
<option value="">--城市--</option>
</select>
<select id="country">
<option value="">--县城--</option>
</select>
</body>
</html>