簡(jiǎn)單實(shí)用jquery版三級(jí)聯(lián)動(dòng)select示例
更新時(shí)間:2013年07月04日 16:58:19 作者:
本文主要為大家介紹下通過jquery實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)select這里用到的json文件,只是事例,根據(jù)情況添加或編寫,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助
html和js部分
復(fù)制代碼 代碼如下:
<!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>請(qǐng)選擇</option>
</select>
<select class="city">
<option>請(qǐng)選擇</option>
</select>
<select class="district">
<option>請(qǐng)選擇</option>
</select>
</div>
<div class="selectList">
<select class="province">
<option>請(qǐng)選擇</option>
</select>
<select class="city">
<option>請(qǐng)選擇</option>
</select>
<select class="district">
<option>請(qǐng)選擇</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");
//初始化省
var province = function(){
$.each(areaJson,function(i,province){
temp_html+="<option value='"+province.p+"'>"+province.p+"</option>";
});
oProvince.html(temp_html);
city();
};
//賦值市
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();
};
//賦值縣
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);
};
};
//選擇省改變市
oProvince.change(function(){
city();
});
//選擇市改變縣
oCity.change(function(){
district();
});
//獲取json數(shù)據(jù)
$.getJSON(url,function(data){
areaJson = data;
province();
});
});
});
</script>
</body>
</html>
json文件(area.json),這里只是事例,根據(jù)情況添加或編寫
復(fù)制代碼 代碼如下:
[
{"p":"江西省",
"c":[
{"ct":"南昌市",
"d":[
{"dt":"西湖區(qū)"},
{"dt":"東湖區(qū)"},
{"dt":"高新區(qū)"}
]},
{"ct":"贛州市",
"d":[
{"dt":"瑞金縣"},
{"dt":"南豐縣"},
{"dt":"全南縣"}
]}
]},
{"p":"北京",
"c":[
{"ct":"東城區(qū)"},
{"ct":"西城區(qū)"}
]},
{"p":"河北省",
"c":[
{"ct":"石家莊",
"d":[
{"dt":"長(zhǎng)安區(qū)"},
{"dt":"橋東區(qū)"},
{"dt":"橋西區(qū)"}
]},
{"ct":"唐山市",
"d":[
{"dt":"灤南縣"},
{"dt":"樂亭縣"},
{"dt":"遷西縣"}
]}
]}
]
各位最好自己封裝成插件,方便調(diào)用
您可能感興趣的文章:
- jquery+json 通用三級(jí)聯(lián)動(dòng)下拉列表
- 省市區(qū)三級(jí)聯(lián)動(dòng)jquery實(shí)現(xiàn)代碼
- jQuery select表單提交省市區(qū)城市三級(jí)聯(lián)動(dòng)核心代碼
- jQuery實(shí)現(xiàn)的省市縣三級(jí)聯(lián)動(dòng)菜單效果完整實(shí)例
- jQuery+jsp實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果(附源碼)
- asp.net省市三級(jí)聯(lián)動(dòng)的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- 基于jQuery+JSON的省市二三級(jí)聯(lián)動(dòng)效果
- JSON+Jquery省市區(qū)三級(jí)聯(lián)動(dòng)
- jquery實(shí)現(xiàn)的省市區(qū)三級(jí)聯(lián)動(dòng)
- jQuery實(shí)現(xiàn)簡(jiǎn)單三級(jí)聯(lián)動(dòng)效果
相關(guān)文章
jQuery+CSS實(shí)現(xiàn)一個(gè)側(cè)滑導(dǎo)航菜單代碼
側(cè)滑菜單在網(wǎng)站設(shè)計(jì)中應(yīng)用比較廣泛,在許多網(wǎng)站上都可以看到此種類型的菜單。本文給大家介紹jQuery+CSS實(shí)現(xiàn)一個(gè)側(cè)滑導(dǎo)航菜單代碼,需要的朋友參考下吧2016-05-05
自動(dòng)設(shè)置iframe大小的jQuery代碼
自動(dòng)設(shè)置iframe的寬度在應(yīng)用中還是比較廣泛的,本文使用jquery實(shí)現(xiàn)一下,感興趣的朋友可以參考下2013-09-09
jQuery+css實(shí)現(xiàn)炫目的動(dòng)態(tài)塊漂移效果
這篇文章主要介紹了jQuery+css實(shí)現(xiàn)的動(dòng)態(tài)塊漂移效果,涉及jQuery基于隨機(jī)數(shù)與時(shí)間函數(shù)動(dòng)態(tài)操作頁面元素樣式的相關(guān)技巧,需要的朋友可以參考下2016-01-01
js前臺(tái)判斷開始時(shí)間是否小于結(jié)束時(shí)間
js前臺(tái)判斷開始時(shí)間是否小于結(jié)束時(shí)間,結(jié)合了jquery需要的朋友可以參考下2012-02-02
基于jQuery和Bootstrap框架實(shí)現(xiàn)仿知乎前端動(dòng)態(tài)列表效果
最近基于jQuery和Bootstrap框架實(shí)現(xiàn)了一個(gè)仿知乎動(dòng)態(tài)列表的前端效果,基本實(shí)現(xiàn)了和知乎動(dòng)態(tài)列表相同的效果,下面小編通過本文給大家分享實(shí)現(xiàn)思路及代碼,對(duì)bootstrap 實(shí)現(xiàn)仿知乎前端動(dòng)態(tài)列表效果感興趣的朋友一起看看吧2016-11-11
運(yùn)用jQuery寫的驗(yàn)證表單(實(shí)例講解)
下面小編就為大家?guī)硪黄\(yùn)用jQuery寫的驗(yàn)證表單(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
jQuery 位置函數(shù)offset,innerWidth,innerHeight,outerWidth,outerHei
jQuery的位置函數(shù)(offset(),innerWidth(),innerHeight(),outerWidth(),outerHeight(),scrollTop(),scrollLeft())小應(yīng)用2010-03-03

