Home  >  Article  >  Web Front-end  >  Province and city linkage code based on jquery & json_jquery

Province and city linkage code based on jquery & json_jquery

WBOY
WBOYOriginal
2016-05-16 17:52:10973browse

Effect demonstration:



html code:

Copy code The code is as follows:





Provincial and municipal linkage

< ;script src="/Scripts/script.js" type="text/javascript">


Demo:














script.js code:

Copy code The code is as follows:

/*
author: elycir
create: 2012-06
description: Provincial and municipal level three (secondary) linkage
* /
$(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 = " ";
var initCity = "";
var initDistrict = "";
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 container
var s; // s: selected identifier
var pre; // pre: initial value
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 = '';
}
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 = '';
}
}

}
if (initoption !== '') {
targetobj.html(initoption t);
} else {
targetobj.html(t);
}
},
"json"
);
}
};
} ();
citySelector.Init();
});

Province and city json data file: Click to download
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn