select.js
1.3 KB
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
var province=$("#province"),city=$("#city"),town=$("#town");
for(var i=0;i<provinceList.length;i++){
addEle(province,provinceList[i].name);
}
function addEle(ele,value){
var optionStr="";
optionStr="<option value="+value+">"+value+"</option>";
ele.append(optionStr);
}
function removeEle(ele){
ele.find("option").remove();
var optionStar="<option value="+"请选择"+">"+"请选择"+"</option>";
ele.append(optionStar);
}
var provinceText,cityText,cityItem;
province.on("change",function(){
provinceText=$(this).val();
$.each(provinceList,function(i,item){
if(provinceText == item.name){
cityItem=i;
return cityItem
}
});
removeEle(city);
removeEle(town);
$.each(provinceList[cityItem].cityList,function(i,item){
addEle(city,item.name)
})
});
city.on("change",function(){
cityText=$(this).val();
removeEle(town);
$.each(provinceList,function(i,item){
if(provinceText == item.name){
cityItem=i;
return cityItem
}
});
$.each(provinceList[cityItem].cityList,function(i,item){
if(cityText == item.name){
for(var n=0;n<item.areaList.length;n++){
addEle(town,item.areaList[n])
}
}
});
});