基于jquery & json的省市區(qū)聯(lián)動代碼
更新時間:2012年06月26日 20:34:43 作者:
基于jquery & json的省市區(qū)聯(lián)動代碼,需要的朋友可以參考下
效果演示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>省市區(qū)聯(lián)動</title>
<script src="/Scripts/jquery.min.js" type="text/javascript"></script>
<script src="/Scripts/script.js" type="text/javascript"></script>
</head>
<body>
<h2>Demo:</h2>
<select id="province">
<option value="0">請選擇省份</option>
</select>
<select id="city">
<option value="0">請選擇城市</option>
</select>
<select id="district">
<option value="0">請選擇區(qū)縣</option>
</select>
<!--下列為初始值(可選,編輯表單時設置)-->
<input type="hidden" value="440000" id="pre_province"/>
<input type="hidden" value="440500" id="pre_city"/>
<input type="hidden" value="440511" id="pre_district"/>
</body>
</html>
script.js代碼:
/*
author: elycir
create: 2012-06
description: 省市區(qū)三級(二級)聯(lián)動
*/
$(function () {
var citySelector = function () {
var province = $("#province");
var city = $("#city");
var district = $("#district");
var preProvince = $("#pre_province");
var preCity = $("#pre_city");
var preDistrict = $("#pre_district");
var jsonProvince = "/content/json-array-of-province.js";
var jsonCity = "/content/json-array-of-city.js";
var jsonDistrict = "/content/json-array-of-district.js";
var hasDistrict = true;
var initProvince = "<option value='0'>請選擇省份</option>";
var initCity = "<option value='0'>請選擇城市</option>";
var initDistrict = "<option value='0'>請選擇區(qū)縣</option>";
return {
Init: function () {
var that = this;
that._LoadOptions(jsonProvince, preProvince, province, null, 0, initProvince);
province.change(function () {
that._LoadOptions(jsonCity, preCity, city, province, 2, initCity);
});
if (hasDistrict) {
city.change(function () {
that._LoadOptions(jsonDistrict, preDistrict, district, city, 4, initDistrict);
});
province.change(function () {
city.change();
});
}
province.change();
},
_LoadOptions: function (datapath, preobj, targetobj, parentobj, comparelen, initoption) {
$.get(
datapath,
function (r) {
var t = ''; // t: html容器
var s; // s: 選中標識
var pre; // pre: 初始值
if (preobj === undefined) {
pre = 0;
} else {
pre = preobj.val();
}
for (var i = 0; i < r.length; i++) {
s = '';
if (comparelen === 0) {
if (pre !== "" && pre !== 0 && r[i].code === pre) {
s = ' selected=\"selected\" ';
pre = '';
}
t += '<option value=' + r[i].code + s + '>' + r[i].name + '</option>';
}
else {
var p = parentobj.val();
if (p.substring(0, comparelen) === r[i].code.substring(0, comparelen)) {
if (pre !== "" && pre !== 0 && r[i].code === pre) {
s = ' selected=\"selected\" ';
pre = '';
}
t += '<option value=' + r[i].code + s + '>' + r[i].name + '</option>';
}
}
}
if (initoption !== '') {
targetobj.html(initoption + t);
} else {
targetobj.html(t);
}
},
"json"
);
}
};
} ();
citySelector.Init();
});
省市區(qū)json數(shù)據(jù)文件:點擊下載
html代碼:
復制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>省市區(qū)聯(lián)動</title>
<script src="/Scripts/jquery.min.js" type="text/javascript"></script>
<script src="/Scripts/script.js" type="text/javascript"></script>
</head>
<body>
<h2>Demo:</h2>
<select id="province">
<option value="0">請選擇省份</option>
</select>
<select id="city">
<option value="0">請選擇城市</option>
</select>
<select id="district">
<option value="0">請選擇區(qū)縣</option>
</select>
<!--下列為初始值(可選,編輯表單時設置)-->
<input type="hidden" value="440000" id="pre_province"/>
<input type="hidden" value="440500" id="pre_city"/>
<input type="hidden" value="440511" id="pre_district"/>
</body>
</html>
script.js代碼:
復制代碼 代碼如下:
/*
author: elycir
create: 2012-06
description: 省市區(qū)三級(二級)聯(lián)動
*/
$(function () {
var citySelector = function () {
var province = $("#province");
var city = $("#city");
var district = $("#district");
var preProvince = $("#pre_province");
var preCity = $("#pre_city");
var preDistrict = $("#pre_district");
var jsonProvince = "/content/json-array-of-province.js";
var jsonCity = "/content/json-array-of-city.js";
var jsonDistrict = "/content/json-array-of-district.js";
var hasDistrict = true;
var initProvince = "<option value='0'>請選擇省份</option>";
var initCity = "<option value='0'>請選擇城市</option>";
var initDistrict = "<option value='0'>請選擇區(qū)縣</option>";
return {
Init: function () {
var that = this;
that._LoadOptions(jsonProvince, preProvince, province, null, 0, initProvince);
province.change(function () {
that._LoadOptions(jsonCity, preCity, city, province, 2, initCity);
});
if (hasDistrict) {
city.change(function () {
that._LoadOptions(jsonDistrict, preDistrict, district, city, 4, initDistrict);
});
province.change(function () {
city.change();
});
}
province.change();
},
_LoadOptions: function (datapath, preobj, targetobj, parentobj, comparelen, initoption) {
$.get(
datapath,
function (r) {
var t = ''; // t: html容器
var s; // s: 選中標識
var pre; // pre: 初始值
if (preobj === undefined) {
pre = 0;
} else {
pre = preobj.val();
}
for (var i = 0; i < r.length; i++) {
s = '';
if (comparelen === 0) {
if (pre !== "" && pre !== 0 && r[i].code === pre) {
s = ' selected=\"selected\" ';
pre = '';
}
t += '<option value=' + r[i].code + s + '>' + r[i].name + '</option>';
}
else {
var p = parentobj.val();
if (p.substring(0, comparelen) === r[i].code.substring(0, comparelen)) {
if (pre !== "" && pre !== 0 && r[i].code === pre) {
s = ' selected=\"selected\" ';
pre = '';
}
t += '<option value=' + r[i].code + s + '>' + r[i].name + '</option>';
}
}
}
if (initoption !== '') {
targetobj.html(initoption + t);
} else {
targetobj.html(t);
}
},
"json"
);
}
};
} ();
citySelector.Init();
});
省市區(qū)json數(shù)據(jù)文件:點擊下載
您可能感興趣的文章:
- js實現(xiàn)的全國省市二級聯(lián)動下拉選擇菜單完整實例
- PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動
- 最好用的省市二級聯(lián)動 原生js實現(xiàn)你值得擁有
- JS實多級聯(lián)動下拉菜單類,簡單實現(xiàn)省市區(qū)聯(lián)動菜單!
- javascript實現(xiàn)省市區(qū)三級聯(lián)動下拉框菜單
- 省市區(qū)三級聯(lián)動下拉框菜單javascript版
- js實現(xiàn)一個省市區(qū)三級聯(lián)動選擇框代碼分享
- 全國省市二級聯(lián)動下拉菜單 js版
- 從QQ網(wǎng)站中提取的純JS省市區(qū)三級聯(lián)動菜單
- js實現(xiàn)省級聯(lián)動(數(shù)據(jù)結構優(yōu)化)
相關文章
jQuery操作input type=radio的實現(xiàn)代碼
jQuery操作input type=radio的實現(xiàn)代碼,需要的朋友可以參考下2012-06-06jQuery實現(xiàn)ajax的疊加和停止(終止ajax請求)
這篇文章主要介紹了jQuery實現(xiàn)ajax的疊加和停止的關鍵代碼,代碼簡單易懂,需要的朋友可以參考下2016-08-08jQuery常用樣式操作實例分析(獲取、設置、追加、刪除、判斷等)
這篇文章主要介紹了jQuery常用樣式操作,結合實例形式分析了jQuery針對頁面元素樣式的獲取、設置、追加、刪除、判斷等操作方法,需要的朋友可以參考下2016-09-09jQuery實現(xiàn)CheckBox全選、全不選功能
這篇文章主要介紹了jQuery實現(xiàn)CheckBox全選、全不選功能,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-01-01