var proExp = '.+[省]', cityExp = '.+[市]', disExp = '.+[区]'
var province = address.match(new RegExp(proExp)) // 省
var city = address.match(new RegExp(cityExp))[0].replace(province, '') // 市
var district = address.match(new RegExp(disExp))[0].replace(province, '').replace(city, '') // 区
1.首先写一个js文件,areaLocation.js:
var
provinceArray=
new
Array()
provinceArray=[
{"pname":"江苏",
"country":[
{
"cname":"南京市",
"town":["玄武区","白下区","秦淮区","建邺区","雨花台","江宁区",
"六合区","溧水县","高淳县"]},
{
"cname":"南通市",
"town":["崇川区","港闸区","启东市","如皋市","通州市","海门市","海安县","如东县"]
},
{
"cname":"苏州市",
"town":["金阊区","沧浪区","平江区","虎丘区","吴中区","张家港","昆山市","吴江市","太仓市"]
}
]
},
{"pname":"上海",
"country":[
{
"cname":"上海市",
"town":["徐汇区","普陀区","杨浦区","虹口区","卢湾区","浦东区","黄浦区","长宁区","闸北区","静安区","闵行区","松江区"]
}
]
}
]
function
areaInitialize(){
province.length=1
for(var
i
=
0i<provinceArray.lengthi++){
var
pro
=
provinceArray[i].pname
province.options[province.length]
=
new
Option(pro,pro)
}
}
function
changeCLocation(id,city){
//alert(id)
city.length
=
0
if(id>0){
var
j=id-1
var
i
var
countrys
=
new
Array()
countrys
=
provinceArray[j].country
city.options[0]
=
new
Option('不限','')
for
(i=0i
<
countrys.length
i++){
var
cname
=
countrys[i].cname
city.options[city.length]
=
new
Option(cname,
cname)
}
changeTLocation(id,1,town)
}else
if(id==0){
city.length
=
0
town.length
=
0
city.options[city.length]
=
new
Option('不限','')
town.options[town.length]
=
new
Option('不限','')
}
}
function
changeTLocation(pid,cid,town){
//alert(cid)
var
countrys
=
new
Array()
var
towns
=
new
Array()
town.length
=
0
var
i
countrys
=
provinceArray[pid-1].country
areaId
=
countrys[cid-1].areaId
towns
=
countrys[cid-1].town
city.options[0]
=
new
Option('不限','')
town.options[0]
=
new
Option('不限','')
for
(i=0i
<
towns.length
i++){
var
tname
=
towns[i]
town.options[town.length]
=
new
Option(tname,tname)
}
town.options[0]
=
new
Option('不限','')
}
2.
再写一个html页面:
<!DOCTYPE
HTML
PUBLIC
"-//W3C//DTD
HTML
4.01
Transitional//EN">
<html>
<head>
<script
type="text/javascript"
></script>
<script
type="text/javascript">
window.onload=areaInitialize()
</script>
</head>
<body>
<select
size="1">
<option
value=0
selected>--请选择--</option>
</select>
<select
size="1"
>
<option
value=0
selected>--请选择--</option>
</select>
<select
size=1>
<option
value=0
selected>--请选择--</option>
</select>
</body>
</html>
http://download.csdn.net/detail/liujian_01/4974474你看看这个吧,就是第一个变动触发onchange事件,获取对应的二级地区,上面是用ajax从数据库获取二级地区,然后把二级下拉option清空,往Select对象中加入获取到的二级地区