基本完成,有bug

This commit is contained in:
landaiqing 2023-05-01 21:33:04 +08:00
parent c41689b828
commit 71d37fac5f
2 changed files with 68 additions and 26 deletions

View File

@ -13,10 +13,10 @@
<div> <div>
<form> <form>
省份: 省份:
<select name="province" id="province" onchange="showCity()"> <select name="province" id="province">
<option value="0">--请选择--</option> <option value="0">--请选择--</option>
</select>&nbsp;&nbsp; 城市: </select>&nbsp;&nbsp; 城市:
<select name="city" id="city" onchange="showArea()"> <select name="city" id="city">
<option value="0">--请选择--</option> <option value="0">--请选择--</option>
</select>&nbsp;&nbsp; 地区: </select>&nbsp;&nbsp; 地区:
<select name="area" id="area"> <select name="area" id="area">
@ -28,6 +28,16 @@
<script> <script>
$(function(){ $(function(){
getCountry(); getCountry();
$("#province").change(function(){
CountryId=$("#province").val();
// console.log(FatherId);
getCity(CountryId);
CityID=$("#city").val();
console.log(CityID);
getArea(CityID);
});
}) })
</script> </script>
</html> </html>

View File

@ -1,4 +1,5 @@
function getCountry(code){
function getData(){
$.ajax({ $.ajax({
url : './json/third-level-address.json', url : './json/third-level-address.json',
type : 'get', type : 'get',
@ -8,6 +9,24 @@ function getCountry(code){
alert("系统错误"); alert("系统错误");
return; return;
} }
dataJson=data;
}
});
return dataJson;
}
function getCountry(){
// $.ajax({
// url : './json/third-level-address.json',
// type : 'get',
// async: false,
// success : function(data) {
// if(data=="error"){
// alert("系统错误");
// return;
// }
var data = getData();
var country = new Array(); var country = new Array();
var children = new Array(); var children = new Array();
var countryCode=new Array(); var countryCode=new Array();
@ -16,25 +35,38 @@ function getCountry(code){
country.push(tmp.name); country.push(tmp.name);
children.push(tmp.children); children.push(tmp.children);
}); });
$('#country').show().html(country); // for(var i in data){
// console.log(children); // console.log(data[i].children[0].children[0].code);
// }
// console.log(data[0].children[1].name);
var options = "<option value='0'>--请选择--</option>"; var options = "<option value='0'>--请选择--</option>";
for(var i in data){ for(var i in data){
if(code && code == data[i].code ){
options += "<option value='" + data[i].code + "' selected>" + dataJson[i].name+"</option>"; options += "<option value='" + data[i].code + "' selected>" + dataJson[i].name+"</option>";
}else{
options += "<option value='" + data[i].code + "' >" + data[i].name+"</option>";
}
$("#province").html(options); $("#province").html(options);
// console.log(data[i].code); // console.log(data[i].code);
} }
},
error : function(data) {
html = "<option value='0'>--请选择--</option>";
} }
})
}
function showCity(){ function getCity(countryCode){
var data = getData();
$('#city').empty();
var options = "<option value='0'>--请选择--</option>";
for(var i in data){
if(countryCode && countryCode == data[i].code){
options += "<option value='" + data[i].children[0].code + "' selected>" + data[i].children[0].name+"</option>";
}
$("#city").html(options);
}
}
function getArea(cityCode){
var data = getData();
var options = "<option value='0'>--请选择--</option>";
for(var i in data){
if(cityCode && cityCode == data[i].children[0].code){
options += "<option value='" + data[i].children[0].children[0].code + "' selected>" + data[i].children[0].children[0].name+"</option>";
}
$("#area").html(options);
}
} }