下面小編就為大家?guī)?lái)一篇省市選擇的簡(jiǎn)單實(shí)現(xiàn)(基于zepto.js)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。
效果如下:
<div class="clList overflow-h mt75">
<select class="pull-left cl-35 select01" id="cityParent">
<option>請(qǐng)選擇省</option>
</select>
<select class="pull-left cl-35 select02" id="city">
<option>請(qǐng)選擇市</option>
</select>
</div>
--------------------------------------------------
//測(cè)試數(shù)據(jù)
var _json = [ {
"areas" : [ {
"name" : "東城區(qū)"
}, {
"name" : "西城區(qū)"
}, {
"name" : "崇文區(qū)"
}, {
"name" : "宣武區(qū)"
}, {
"name" : "昌平區(qū)"
}, {
"name" : "朝陽(yáng)區(qū)"
}, {
"name" : "海淀區(qū)"
}, {
"name" : "豐臺(tái)區(qū)"
}, {
"name" : "石景山區(qū)"
}, {
"name" : "門頭溝區(qū)"
}, {
"name" : "房山區(qū)"
}, {
"name" : "通州區(qū)"
}, {
"name" : "順義區(qū)"
}, {
"name" : "大興區(qū)"
}, {
"name" : "平谷區(qū)"
}, {
"name" : "懷柔區(qū)"
}, {
"name" : "密云縣"
}, {
"name" : "延慶縣"
} ],
"name" : "北京市"
},{
"areas" : [ {
"name" : "合肥市"
}, {
"name" : "滁州市"
}, {
"name" : "蚌埠市"
}, {
"name" : "蕪湖市"
}, {
"name" : "淮南市"
}, {
"name" : "淮北市"
}, {
"name" : "馬鞍山市"
}, {
"name" : "安慶市"
}, {
"name" : "宿州市"
}, {
"name" : "阜陽(yáng)市"
}, {
"name" : "亳州市"
}, {
"name" : "黃山市"
}, {
"name" : "銅陵市"
}, {
"name" : "宣城市"
}, {
"name" : "六安市"
}, {
"name" : "池州市"
} ],
"name" : "安徽省"
}, {
"areas" : [ {
"name" : "福州市"
}, {
"name" : "廈門市"
}, {
"name" : "寧德市"
}, {
"name" : "莆田市"
}, {
"name" : "泉州市"
}, {
"name" : ">"
}, {
"name" : "漳州市"
}, {
"name" : "龍巖市"
}, {
"name" : "三明市"
}, {
"name" : "南平市"
} ],
"name" : "福建省"
}, {
"areas" : [ {
"name" : "蘭州市"
}, {
"name" : "臨夏州"
}, {
"name" : "定西市"
}, {
"name" : "平?jīng)鍪?
}, {
"name" : "慶陽(yáng)市"
}, {
"name" : "武威市"
}, {
"name" : "金昌市"
}, {
"name" : "張掖市"
}, {
"name" : "酒泉市"
}, {
"name" : "嘉峪關(guān)市"
}, {
"name" : "天水市"
}, {
"name" : "隴南市"
}, {
"name" : "甘南州"
}, {
"name" : "白銀市"
} ],
"name" : "甘肅省"
}];
function loadCityPar() {
var cityPar = '<option value="0">請(qǐng)選擇省</option>';
var lenPar = _json.length; //省市數(shù)量
for(var i=0; i<lenPar; i++){
var n = i + 1;
cityPar += '<option value="'+n+'">'+_json[i].name+'</option>';
}
$('#cityParent').html(cityPar);
}
loadCityPar();
$(page).on('change','#cityParent',function () {
var index = $('#cityParent option').not(function(){ return !this.selected }).index();
var i = index - 1;
if(i >= 0){
var city = '<option value="0">請(qǐng)選擇市</option>';
var len = _json[i].areas.length;
for(var j=0; j<len; j++){
var m = j + 1;
city += '<option value="'+m+'">'+_json[i].areas[j].name+'</option>';
}
$('#city').html(city);
}
});
-------------------------------------------------------------
.wgyHospital .select01,.wgyHospital .select02{ padding-left:.3rem; padding-right:.3rem; height:1.6rem; line-height:1.6rem; border:1px solid #ccc; text-align:center;}
.wgyHospital .select01{ border-radius:.75rem 0 0 .75rem;}
.wgyHospital .select02{ border-radius:0 .75rem .75rem 0; border-left:none;}
以上就是小編為大家?guī)?lái)的省市選擇的簡(jiǎn)單實(shí)現(xiàn)(基于zepto.js)的全部?jī)?nèi)容了