【JavaScript】2018最新的省市區,獲取json,實現三級聯動選擇省市區(底部原始碼地址)
阿新 • • 發佈:2019-01-26
注意:因為getJson()方法是不能獲取本地檔案的,所以頁面必須要在本地伺服器開啟,否則會產生跨域問題。
伺服器開啟頁面
本地file模式開啟頁面
程式碼應用部分
_loadRegion(localStorage.language,function(){
publicRegionObj = opreateRegion($('#province'),$('#city'),$('#county'));
});
方法編寫部分
var publicCityObj = null;
// 載入城市
//新的省市區
var _loadRegion = function (_language, callback) {
if (publicCityObj) { //如果這個物件存在,說明已經執行過該函式,可以直接退出
return;
}
var provinces = [];
var city = [];
var area = [];
var cityCache = {};
var areaCache = {};
var getcityNameCache = {};
var getProvinceNameCache = {};
var getAreaNameCache = {};
var allRegionCache = {};
var i = 0;
var allRegion = null;
var loadRegionSuccess = function() {
i++;
if (i == 3) {
publicCityObj = obj;
allRegion = [].concat(provinces, city, area);
if (callback) { callback() }
}
}
$.getJSON('../js/city/provinces_' + _language + '.json', function(resdata) {
provinces = resdata;
loadRegionSuccess()
})
var isLoadCityBack = $.getJSON('../js/city/cities_' + _language + '.json', function(resdata) {
city = resdata;
$.each(resdata, function(index, item) {
if (!cityCache[item.parent_code]) {
cityCache[item.parent_code] = [];
}
cityCache[item.parent_code].push(item)
})
loadRegionSuccess()
})
$.getJSON('../js/city/areas_' + _language + '.json', function(resdata) {
area = resdata;
$.each(resdata, function(index, item) {
if (!areaCache[item.parent_code]) {
areaCache[item.parent_code] = [];
}
areaCache[item.parent_code].push(item)
})
loadRegionSuccess();
})
var obj = {
getRegionName: function(code) {
if (!code) {
return console.log('請輸入地區碼!')
}
if (allRegionCache[code]) {
return allRegionCache[code];
}
var _regionName = '';
$.each(allRegion, function(index, item) {
if (item.code == code) {
_regionName = item.name;
}
})
return allRegionCache[code] = _regionName;
},
getProvinces: function() {
return provinces;
},
getCityName: function(cityCode) {
if (!cityCode) {
return console.log('獲取市失敗, 請傳入市碼!');
}
if (getcityNameCache[cityCode]) {
return getcityNameCache[cityCode];
}
var _cityName = ''
$.each(city, function(index, item) {
if (cityCode == item.code) {
_cityName = item.name;
}
})
return getcityNameCache[cityCode] = _cityName;
},
getAreaName: function(areaCode) {
if (!areaCode) {
return console.log('獲取市失敗, 請傳入區碼!');
}
if (getAreaNameCache[areaCode]) {
return getAreaNameCache[areaCode];
}
var _areaName = ''
$.each(area, function(index, item) {
if (areaCode == item.code) {
_areaName = item.name;
}
})
return getAreaNameCache[areaCode] = _areaName;
},
getProvinceName: function(provinceCode) {
if (!provinceCode) {
return console.log('獲取市失敗, 請傳入省碼!');
}
if (getProvinceNameCache[provinceCode]) {
return getProvinceNameCache[provinceCode];
}
var _proviceName = ''
$.each(provinces, function(index, item) {
if (provinceCode == item.code) {
_proviceName = item.name;
}
})
return getProvinceNameCache[provinceCode] = _proviceName;
},
getCities: function(parentCode) {
if (!parentCode) {
return console.log('獲取市失敗, 請傳入省碼!');
}
//判斷有沒有快取,有就直接返回
if (cityCache[parentCode]) {
return cityCache[parentCode]
}
var _city_arr = [];
$.each(city, function(index, item) {
if (item.parent_code == parentCode) {
_city_arr.push(item);
}
})
return cityCache[parentCode] = _city_arr;
},
getAreas: function(parentCode) {
if (!parentCode) {
return console.log('獲取獲取縣, 請傳入市碼!');;
}
//判斷有沒有快取,有就直接返回
if (areaCache[parentCode]) {
return areaCache[parentCode]
}
var _ares_arr = [];
$.each(area, function(index, item) {
if (item.parent_code == parentCode) {
_ares_arr.push(item);
}
})
return areaCache[parentCode] = _ares_arr;
}
}
}
var opreateRegion = function(provinceDom, cityDom, AreaDom) {
var addRegion = function(dom, provinceArr) {
if (!dom || !provinceArr) {
return console.log('')
}
dom.find('option[data-add]').remove();
var _htm = '';
$.each(provinceArr, function(index, item) {
_htm += '<option data-add value="' + item.code + '">' + item.name + '</option>'
})
dom.append(_htm);
}
addRegion(provinceDom, publicCityObj.getProvinces());
provinceDom.on('change', function(e) {
oooo.init()
oooo.changeProvince($(this).val());
})
cityDom.on('change', function() {
if($(this).val() == ""){
AreaDom.find("option").prop("selected",false);
}else{
oooo.changeCity($(this).val())
}
})
var oooo = {
init: function() {
cityDom.find('option[data-add]').remove().removeClass('hidden');
AreaDom.find('option[data-add]').remove().removeClass('hidden');
},
changeProvince: function(code) {
var arr = publicCityObj.getCities(code);
if (arr.length > 0) {
cityDom.removeClass('hidden');
AreaDom.removeClass('hidden');
cityDom.find('option[data-add]').remove();
addRegion(cityDom, arr)
} else {
cityDom.addClass('hidden');
AreaDom.addClass('hidden');
}
},
changeCity: function(code) {
var arr = publicCityObj.getAreas(code);
if (arr.length > 0) {
AreaDom.removeClass('hidden');
AreaDom.find('option[data-add]').remove();
addRegion(AreaDom, arr)
} else {
AreaDom.addClass('hidden');
}
},
inputRegionData: function(provincecode, citycode, areacode) {
if (!provincecode) {
return;
}
provinceDom.find('option[value="' + provincecode + '"]').prop('selected', true);
if (!citycode) {
return;
}
addRegion(cityDom, publicCityObj.getCities(provincecode))
cityDom.find('option[value="' + citycode + '"]').prop('selected', true);
if (!areacode) {
return;
}
addRegion(AreaDom, publicCityObj.getAreas(citycode))
AreaDom.find('option[value="' + areacode + '"]').prop('selected', true);
}
}
return oooo;
}
獲取返回的資料進行頁面地址的渲染
//解決省、市同名都出現的問題
function fixCity(address){
if(isUndefined(address)){return address;}
var res="";
var adr=address.split(',');
var adr0 = publicCityObj.getRegionName(adr[0]);
var adr1 = publicCityObj.getCityName(adr[1]);
var adr2 = publicCityObj.getAreaName(adr[2]);
if(adr0 == undefined){
adr0 = "";
}
if(adr1 == undefined){
adr1 = "";
}
if(adr2 == undefined){
adr2 = "";
}
res = adr0 + adr1 + adr2;
// if(adr[0]==adr[1]){
// res=adr[0]+adr[2]+adr[3];
// }else{
// res=adr[0]+adr[1]+adr[2]+adr[3];
// }
return res;
}
原始碼地址