DragonWar% 2014-05-18 16:30 采纳率: 0%
浏览 31

onchange javascript和ajax

When i run it on server doesnt retrieve any result from my xml files and doesn`t give any xmlHttpRequest error on "Google Chrome Developer Tools". Do i need to use "onclick" event on every "option" or i need to use "onchange" event.

I want to make it when an option is checked or selected, to send XMLHttpRequest to server and retrieve the informations (city, village ,street, code) from the specified XML file.

function showTowns()
{
    var xhr;
    var selectValue;
    selectValue=document.getElementById("selectBox").value;

    var url1="countriesItaXML.xml";
    var url2="countriesMakXML.xml";
    var url3="countriesUSAXML.xml";
    var txt,city,village,street,country,x,xx;
    if(window.XMLHttpRequest)
        {
            xhr= new XMLHttpRequest();
        }
    else
    {
        xhr= new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.onreadystatechange=function()
    {
        if(xhr.readyState==4 && xhr.status==200)
            {
                if(selectValue=="o1")
                {
                    x=xhr.responseXML.documentElement.getElementsByTagName("country");
                    for(i=0;i<x.length;i++)
                        {
                        city="";
                        xx=x[i].getElementsByTagName("city");
                        {
                            try
                            {
                                city=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                city="undefined";
                            }
                        }
                        village="";
                        xx=x[i].getElementsByTagName("village");
                        {
                            try
                            {
                                village=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                village="undefined";
                            }
                        }
                        street="";
                        xx=x[i].getElementsByTagName("street");
                        {
                            try
                            {
                                street=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                street="undefined";
                            }
                        }
                        code="";
                        xx=x[i].getElementsByTagName("code");
                        {
                            try
                            {
                                code=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                code="undefined";
                            }
                        }
                        txt="<table border='1'><th>City:</th><td>"+city+"</td></tr><tr><th>Village:</th><td>"+village+"</td></tr><tr><th>Street:</th><td>"+street+"</td></tr><tr><th>Code:</th><td>"+code+"</td></tr></table><br />";
                        document.getElementById('prvGrad').innerHTML=txt;
                        }

                    xhr.open("GET",url1,true);
                    xhr.send();
                }
                else if(selectValue=="o2")
                {
                    x=xhr.responseXML.documentElement.getElementsByTagName("country");
                    for(i=0;i<x.length;i++)
                        {
                        city="";
                        xx=x[i].getElementsByTagName("city");
                        {
                            try
                            {
                                city=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                city="undefined";
                            }
                        }
                        village="";
                        xx=x[i].getElementsByTagName("village");
                        {
                            try
                            {
                                village=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                village="undefined";
                            }
                        }
                        street="";
                        xx=x[i].getElementsByTagName("street");
                        {
                            try
                            {
                                street=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                street="undefined";
                            }
                        }
                        code="";
                        xx=x[i].getElementsByTagName("code");
                        {
                            try
                            {
                                code=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                code="undefined";
                            }
                        }
                        txt="<table border='1'><th>City:</th><td>"+city+"</td></tr><tr><th>Village:</th><td>"+village+"</td></tr><tr><th>Street:</th><td>"+street+"</td></tr><tr><th>Code:</th><td>"+code+"</td></tr></table><br />";
                        document.getElementById('prvGrad').innerHTML=txt;
                        }

                    xhr.open("GET",url2,true);
                    xhr.send();
                }
                else
                {
                    x=xhr.responseXML.documentElement.getElementsByTagName("country");
                    for(i=0;i<x.length;i++)
                        {
                        city="";
                        xx=x[i].getElementsByTagName("city");
                        {
                            try
                            {
                                city=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                city="undefined";
                            }
                        }
                        village="";
                        xx=x[i].getElementsByTagName("village");
                        {
                            try
                            {
                                village=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                village="undefined";
                            }
                        }
                        street="";
                        xx=x[i].getElementsByTagName("street");
                        {
                            try
                            {
                                street=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                street="undefined";
                            }
                        }
                        code="";
                        xx=x[i].getElementsByTagName("code");
                        {
                            try
                            {
                                code=xx[0].firstChild.nodeValue;
                            }
                            catch(er)
                            {
                                code="undefined";
                            }
                        }
                        txt="<table border='1'><th>City:</th><td>"+city+"</td></tr><tr><th>Village:</th><td>"+village+"</td></tr><tr><th>Street:</th><td>"+street+"</td></tr><tr><th>Code:</th><td>"+code+"</td></tr></table><br />";
                        document.getElementById('prvGrad').innerHTML=txt;
                        }

                    xhr.open("GET",url3,true);
                    xhr.send();                 
                }
            }
    }

}
</script>

XML code

<asd>
    <country>
        <city> New Hampshire </city>
        <village> North Conway </village>
        <street> White Mount.High. </street>
        <code> 3254 </code>
    </country>
    <country>
        <city> Massachussets </city>
        <village> Boston </village>
        <street> HelloStreet 85 </street>
        <code> 020200 </code>
    </country>
    <country>
        <city> New York </city>
        <village> New Jersey </village>
        <street> NovaJersovska 85 </street>
        <code> 99952 enter code here</code>
    </country>
</asd>

And HTML code

<select name="countriesList" id="selectBox" onchange="showTowns(this.value)">
                <option value="o1" id="1">Italia</option>
                <option value="o2" id="2" selected="selected">Makedonija</option>
                <option value="o3" id="3">USA</option>
            </select>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何让企业微信机器人实现消息汇总整合
    • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
    • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
    • ¥15 TLE9879QXA40 电机驱动
    • ¥20 对于工程问题的非线性数学模型进行线性化
    • ¥15 Mirare PLUS 进行密钥认证?(详解)
    • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
    • ¥20 想用ollama做一个自己的AI数据库
    • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
    • ¥15 请问怎么才能复现这样的图呀