JQuery实现省市区三级联动

摘要:
[Download the list in China.xml]jquery版本:1.2.6.据说jquery 1.3的异步加载会导致卡问题。

[中国地区列表.xml下载]

jquery版本:1.2.6,传说jquery1.3异步加载会出现卡的情况。

js内容:

JQuery实现省市区三级联动第1张JQuery实现省市区三级联动第2张Code
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代码:

JQuery实现省市区三级联动第3张JQuery实现省市区三级联动第4张Code
<div id="province_city_area"></div>

 调用:

JQuery实现省市区三级联动第5张JQuery实现省市区三级联动第6张Code
取值:
j.Jprov();
j.Jcity();
j.Jarea();

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

免责声明:文章转载自《JQuery实现省市区三级联动》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Web1.0、Web2.0、Web3.0的主要区别gulp打包详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

XML外部实体(XXE)

A4-XML外部实体(XXE) 如果攻击者可以上传XML文档或者在XML文档中添加恶意内容,通过易受攻击的代码、依赖项或集成,他们就能够攻击含有缺陷的XML处理器。 XXE影响-XXE缺陷可用于提取数据、执行远程服务器请求、扫描内部系统、执行拒绝服务攻击和其他攻击。 XML 可扩展标记语言(Extensible Markup Language,XML...

Ajax请求中,contentType和dataType的区别

contentType: (默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数情况。如果你明确地传递了一个content-type给 $.ajax() 那么他必定会发送给服务器(即使没有数据要发送). 其他可选的类型有:form-data x-www-form-orlencod...

Qt 直接构建XML开发web services客户端程序

client.h client.cpp : XML发送与接收 其中 request.setHeader(QNetworkRequest::ContentTypeHeader, "text/xml;charset=utf-8" /*"application/x-www-form-urlencoded"*/); header的value值可以用soapui...

使用JAVA调用KRPANO加密XML

KRPano自带的命令行工具krpanotools可以加密XML,具体的参数说明如下语法:   krpanotools32.exe encrypt [OPTIONS] inputfiles inputfiles 参数可以是任意个数的文件 (支持*) 选项:-h5 … 使用HTML5兼容的加密方式-bin … 使用仅支持Flash的加密方式-p ...

jQuery 时间控件推荐

My97DatePicker  My97DatePicker是一个更全面,更人性化,并且速度一流的日期选择控件。具有强大的日期范围限制功能;自定义事件和丰富的API库;多语言支持和自定义皮肤支持;跨无限级框架显示和自动选择显示位置。My97DatePicker    JS Calendar  JS Calendar是一个支持多种浏览器,多种语言的日...

Android 项目中文件夹的说明与作用(转)

(转自:http://blog.csdn.net/goodshot/article/details/11529731)   Android 项目中文件夹的作用 1. src:存放所有的*.java源程序。 2. gen:为ADT插件自动生成的代码文件保存路径,里面的R.java将保存所有的资源ID。 3. assets:可以存放项目一些较大的资源文件,例...