asp.net jquery 三级联动

41 篇文章 0 订阅

 var j = jQuery.noConflict();
var prov
="";
var city
="";
var area
="";
j(function(){
    j.
get("/Js/Message/address.xml",function(xml){
            j(
"body").data("xml",xml);
            j(
"#province_city_area").append("<select id='_province'></select>&nbsp;<select id='_city'></select><select id='_area'></select>");
            j(
"province",xml).each(function(){
                
if(j(this).attr("province")==prov)
                    j(
"#_province").append("<option value="+j(this).attr("provinceID")+" selected>"+j(this).attr("province")+"</option>");
                
else
                j(
"#_province").append("<option value="+j(this).attr("provinceID")+">"+j(this).attr("province")+"</option>");
            });
//初始省

            LoadCity(j(
"#_province").val().substr(0,3),xml); //初始市 
            LoadArea(j("#_city").val().substr(0,4),xml); //初始区
            
            j(
"#_province").change(function(){
                j(
"#_city").empty();
                var Pvalue 
= j(this).val().substr(0,3);
                
if(j("province[provinceID^="+Pvalue+"] City",xml).length==0){
                    j(
"#_city").hide();
                    j(
"#_area").remove();
                    
return;
                }
else
                    j(
"#_city").show();
                LoadCity(Pvalue,xml);
                LoadArea(j(
"#_city").val().substr(0,4),xml);
            });
            
            j(
"#_city").change(function(){
                LoadArea(j(
this).val().substr(0,4),xml);
            });
        }
    ); 
    
    function LoadCity(Pvalue,xml){
        j(
"#_city").empty();
        j(
"province[provinceID^="+Pvalue+"] City",xml).each(function(){
            
if(j(this).attr("City")==city)
                j(
"#_city").append("<option value="+j(this).attr("CityID")+" selected>"+j(this).attr("City")+"</option>");
            
else
            j(
"#_city").append("<option value="+j(this).attr("CityID")+">"+j(this).attr("City")+"</option>");
        });
    } 
    
    function LoadArea(Cvalue,xml){
        j(
"#_area").remove();
        j(
"#_city").after("&nbsp;<select id=_area></select>");
        j(
"City[CityID^="+Cvalue+"] Piecearea",xml).each(function(){
            
if(j(this).attr("Piecearea")==area)
                j(
"#_area").append("<option selected>"+j(this).attr("Piecearea")+"</option>");
            
else
            j(
"#_area").append("<option>"+j(this).attr("Piecearea")+"</option>");
        });
    }             
});

j.extend({
    Jprov: function(provalue){
        
if(provalue==null)
            
return j("#_province option:selected").text();
        
else{
            prov 
= provalue;
            j(
"#_province option").each(function(){
                
if(j(this).text()==provalue){
                    j(
this).attr("selected",true);
                    j(
"#_province").trigger("change");
                }
            });
        }    
    },
    Jcity: function(cityvalue){
        
if(cityvalue==null)
            
return j("#_city option:selected").text();
        
else{
            city 
= cityvalue;
            j(
"#_city option").each(function(){
                
if(j(this).text()==cityvalue){
                    j(
this).attr("selected",true);
                    j(
"#_city").trigger("change");
                }
            });
        }
    },
    Jarea: function(areavalue){
        
if(areavalue==null)
            
return j("#_area option:selected").text();
        
else{
            area 
= areavalue;
            j(
"#_area option").each(function(){
                
if(j(this).text()==areavalue){
                    j(
this).attr("selected",true);
                }
            });
        }
    }
});

 

 

html代码:


<div id="province_city_area"></div>

 

 

 调用:


取值:
j.Jprov();
j.Jcity();
j.Jarea();

赋值:
j.Jprov(
"广东省");
j.Jcity(
"深圳市");
j.Jarea(
"宝安区");

下载:http://files.cnblogs.com/di305449473/地区列表.xml

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值