手頭做的項目中需要用到地區庫,在網上找了幾個地區庫,感覺不是很好,於是著手自己生 成一個地區庫.地區來自中國統計局網站(資料夠官方)(地址是: http://www.stats.gov.cn/tjbz/xzqhdm/t20080215_402462675.htm),通過文本處理,產生到 資料庫(需要該地區資料庫的朋友可留下郵箱地址,我發給你),做可處理操作.
這次用到的聯動是產生了一個地區的js檔案,原來是採用AJAX實現聯動的,後來感覺產生JS 檔案並不大,所以採用js方式.
下面是js處理代碼(基於JQuery):
<script type="text/javascript">
$(document).ready(function(){
getProvinces();
});
function getProvinces(){
var pro = "";
for(var i = 0 ; i < provinces.length; i++){
pro += "<option>" + provinces[i] + "</option>";
}
$('#province').empty().append(pro);
getCities();
}
function getCities(){
var proIndex = $('#province').attr('selectedIndex');
showCities(proIndex);
getCounties();
}
function showCities(proIndex){
var cit = "";
if(cities[proIndex] == null){
$('#city').empty();
return;
}
for(var i = 0 ;i < cities[proIndex].length ; i++){
cit += "<option>" + cities[proIndex][i] + "</option>";
}
$('#city').empty().append(cit);
}
function getCounties(){
var proIndex = $('#province').attr('selectedIndex');
var citIndex = $('#city').attr('selectedIndex');
showCounties(proIndex,citIndex);
}
function showCounties(proIndex,citIndex){
var cou = "";
if(counties[proIndex][citIndex] == null){
$('#county').empty();
return;
}
for(var i = 0 ;i < counties[proIndex][citIndex].length;i++){
cou += "<option>" + counties[proIndex][citIndex] [i] + "</option>";
}
$('#county').empty().append(cou);
}
</script><select id="province" onchange="getCities()"></select>
<select id="city" onchange="getCounties() " style="width:120px;"></select>
<select id="county" style="width:120px;"></select>
這樣實現了一個比較好的地區聯動.
本文配套源碼