$selArea = _this.find('select[name="'+ _options.selArea +'"]'); //区、县列表框name
获取省市区json数据:
$.getJSON(_options.url,function(data){
citys = data;
//执行初始化命令
init();
})初始化命令:
var init = function(){
//初始化默认数据
proHtml = "<option> - 请选择 - </option>";
cityHtml = "<option> - 请选择 - </option>";
for(var i in citys){
if(patternPro.test(i)){ //添加一级列表数据
proHtml += "<option value=""+(type=="code"?i:citys[i])+"" data-code=""+ i +"">"+ citys[i] +"</option>";
}
}
//渲染省份一级列表
$selProvince.html(proHtml);
//渲染城市二级列表
$selCity.html(cityHtml);
//默认隐藏区三级列表
$selArea.hide();
//填写地区编码时,利用编码定位
if(type == 'code' && code){
var c = code - code%1e4;
province = c;
c = code - (code%1e4 ? code%1e2 : code);
city = c;
c = code%1e2 ? code : 0;
area = c;
}
//添加默认初始值
$selProvince.find('option').each(function(){
if(type == 'code' && province != ''){
if(province == $(this).data('code')){
$(this).attr('selected',true);
changeProvince($(this).data('code'));
}
}else if(type == 'name' && province != ''){
if(province == $(this).val()){
$(this).attr('selected',true);
changeProvince($(this).data('code'));
}
}
})
$selCity.find('option').each(function(){
if(type == 'code' && city != ''){
if(city == $(this).data('code')){
$(this).attr('selected',true);
changeCity($(this).data('code'));
}
}else if(type == 'name' && city != ''){
if(city == $(this).val()){
$(this).attr('selected',true);
changeCity($(this).data('code'));
}
}
})
$selArea.find('option').each(function(){
//三级联动时,匹配对应地区
if(selState == 1){
if(type == 'code' && area != ''){
if(area == $(this).data('code')){
$(this).attr('selected',true);
}
}else if(type == 'name' && area != ''){
if(area == $(this).val()){
$(this).attr('selected',true);
}
}
}
})
}










