Simple and practical example of jquery three-level linkage select

Source: Internet
Author: User

Html and js
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = gbk/>
<Title> selectList </title>
<Style type = "text/css">
* {Margin: 0; padding: 0 ;}
. SelectList {width: 200px; margin: 50px auto ;}
</Style>
<Script type = "text/javascript" src = "jquery1.7.1.js"> </script>
</Head>
<Body>
<Div class = "selectList">
<Select class = "province">
<Option> select </option>
</Select>
<Select class = "city">
<Option> select </option>
</Select>
<Select class = "district">
<Option> select </option>
</Select>
</Div>
<Div class = "selectList">
<Select class = "province">
<Option> select </option>
</Select>
<Select class = "city">
<Option> select </option>
</Select>
<Select class = "district">
<Option> select </option>
</Select>
</Div>
<Script type = "text/javascript">
$ (Function (){
$ (". SelectList"). each (function (){
Var url = "area. json ";
Var areaJson;
Var temp_html;
Var oProvince = $ (this). find (". province ");
Var oCity = $ (this). find (". city ");
Var oDistrict = $ (this). find (". district ");
// Initialize the province
Var province = function (){
$. Each (areaJson, function (I, province ){
Temp_html + = "<option value = '" + province. p + "'>" + province. p + "</option> ";
});
OProvince.html (temp_html );
City ();
};
// Assign a City Value
Var city = function (){
Temp_html = "";
Var n = oProvince. get (0). selectedIndex;
$. Each (areaJson [n]. c, function (I, city ){
Temp_html + = "<option value = '" + city. ct + "'>" + city. ct + "</option> ";
});
OCity.html (temp_html );
District ();
};
// County
Var district = function (){
Temp_html = "";
Var m = oProvince. get (0). selectedIndex;
Var n = oCity. get (0). selectedIndex;
If (typeof (areaJson [m]. c [n]. d) = "undefined "){
ODistrict.css ("display", "none ");
} Else {
ODistrict.css ("display", "inline ");
$. Each (areaJson [m]. c [n]. d, function (I, district ){
Temp_html + = "<option value = '" + district. dt + "'>" + district. dt + "</option> ";
});
ODistrict.html (temp_html );
};
};
// Select province to change City
OProvince. change (function (){
City ();
});
// Select the city change County
OCity. change (function (){
District ();
});
// Obtain json data
$. GetJSON (url, function (data ){
AreaJson = data;
Province ();
});
});
});
</Script>
</Body>
</Html>

Json file (area. json). Here is only an example. Add or write the file as needed.
Copy codeThe Code is as follows:
[
{"P": "Jiangxi Province ",
"C ":[
{"Ct": "Nanchang city ",
"D ":[
{"Dt": "Xihu District "},
{"Dt": "Donghu district "},
{"Dt": "High-Tech Zone "}
]},
{"Ct": "Ganzhou City ",
"D ":[
{"Dt": "Ruijin county "},
{"Dt": "Nanfeng county "},
{"Dt": "quannan county "}
]}
]},
{"P": "Beijing ",
"C ":[
{"Ct": "Dongcheng District "},
{"Ct": "Xicheng district "}
]},
{"P": "Hebei Province ",
"C ":[
{"Ct": "Shijiazhuang ",
"D ":[
{"Dt": "Chang 'an district "},
{"Dt": "qiaodong district "},
{"Dt": "qiaoxi district "}
]},
{"Ct": "Tangshan City ",
"D ":[
{"Dt": "luannan county "},
{"Dt": "leting county "},
{"Dt": "qianxi county "}
]}
]}
]

You are advised to encapsulate them as plug-ins for convenient calling.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.