ajax基于xpath的三级联动

来源:互联网 发布:windows系统日志备份 编辑:程序博客网 时间:2024/05/17 05:10
 (function(){
            var docxml;
            var province;
            var city;
            var country;
            window.onload=function(){
                //异步问题注意
                initDoc();
                province=document.getElementById("province");
                province.onchange=getCity;
                city=document.getElementById("city");
                city.onchange=getCountry;
            }
            function getCountry(){
                var xpath="/address/province/city[@value="+this.value+"]/country";
                var cs=getData(xpath);
                country.options.length=1;
                for(var i=0;i<cs.length;i++){
                    var node=document.createElement("option");
                    node.text=cs[i].getAttribute("name");
                    node.value=cs[i].getAttribute("value");
                    country.add(node);
                }
            }
            function getCity(){
                var xPath="/address/province[@value='"+this.value+"']/city";
                var cs=getData(xPath);
                 city=document.getElementById("city");
                country=document.getElementById("country");
                //每次改变就把cn变为1;
                city.options.length=1;
                country.options.length=1;
                for(var i=0;i<cs.length;i++){
                    //创建节点元素
                    var node = document.createElement("option");
                    //内容显示
                    node.text = cs[i].getAttribute("name");
                    //value显示
                    node.value = cs[i].getAttribute("value");
                    //增加在下面html想要的位置
                    city.add(node);
                }
            }
            function initProvince(){
                var xpath="/address/province";
                var provinces=getData(xpath);
                province=document.getElementById("province");
                for(var i=0;i<provinces.length;i++){
                    var node=document.createElement("option");
                    node.text=provinces[i].getAttribute("name");
                    node.value=provinces[i].getAttribute("value");
                    province.add(node);
                }
            }
            function getData(xpath){
                if(window.ActiveXObject){
                    return docxml.selectNodes(xpath);
                }else{
                    var as=new Array();
                    //找docxml中的数据
                    var xh=docxml.evaluate(xpath,docxml,null
                            ,XPathResult.UNORDERED_NODE_ITERATOR_TYPE
                            ,null);
                    var node;
                    while(node=xh.iterateNext()){
                        as.push(node);
                    }
                }
                return as;
            }
            function initDoc(){
                var xhr=CreateXmlHttpServlet();
                xhr.open("POST","Area.xml",true);
                xhr.onreadystatechange=function(){
                    if(xhr.readyState==4&&xhr.status==200){
                        docxml=xhr.responseXML;
                        initProvince();
                    }
                }
                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");
                }
            }
        })();
原创粉丝点击