ajax三级联动

<script type="text/javascript">
    (function(){
        var docXMl;
        var province;
        var citys;
        //上面的是Area.xml文件中的
        var pro1;
        var city1;
        var country1;
        window.onload=function(){
            //异步问题注意
            initProvince();
            pro1= document.getElementById("province");
            pro1.onchange=getCity;
            city1=document.getElementById("city");
            city1.onchange=getCountry;

        }
        function getCountry(){
            var cityValue=this.value;
            var citycs=docXMl.getElementsByTagName("city");
            for(var i=0;i<citycs.length;i++) {
                if (citycs[i].getAttribute("value") == cityValue) {
                    var city2 = citycs[i];
                    break;
                }
            }
            var countryss=city2.getElementsByTagName("country");
            country1=document.getElementById("country");
            country1.options.length=1;
            for(var j=0;j<countryss.length;j++){
                var element=document.createElement("option");
                element.text=countryss[j].getAttribute("name");
                element.value=countryss[j].getAttribute("value");
                country1.add(element);
            }
        }
        function getCity(){
            var proValue=this.value;
            for(var i=0;i<province.length;i++){
                if(province[i].getAttribute("value")==proValue){
                    var pro2=province[i];
                    break;
                }
            }
            citys=pro2.getElementsByTagName("city");
            city1=document.getElementById("city");
            country1=document.getElementById("country");
            city1.options.length=1;
            country1.options.length=1;
            for(var j=0;j<citys.length;j++){
                var element=document.createElement("option");
                element.text=citys[j].getAttribute("name");
                element.value=citys[j].getAttribute("value");
                city1.add(element);
            }
        }
        function initProvince(){
            var xhr=CreateXmlHttpServlet();
            xhr.open("POST","Area.xml",true);
            xhr.onreadystatechange=function(){
                if(xhr.readyState==4&&xhr.status==200){
                   docXMl=xhr.responseXML;
                    province=docXMl.getElementsByTagName("province");
                    pro1=document.getElementById("province");
                    for(var i=0;i<province.length;i++){
                        var element=document.createElement("option");
                        element.text=province[i].getAttribute("name");
                        element.value=province[i].getAttribute("value");
                        pro1.add(element);
                    }
                }
            }
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xhr.send();
        }
        function CreateXmlHttpServlet(){
            if(window.XMLHttpRequest){
                return new XMLHttpRequest();
            }else{
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
    })();
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值