只阅读第一个子节点

时间:2017-05-07 14:57:16

标签: javascript html xml dom

嗨,我是一个新手,我一直在尝试创建一个登录表单,其中用户名和密码存储在xml文件中。但是每当我输入我的用户名和密码时,它只允许用户名A和密码12345成功登录。但如果我键入B密码12345.Its不再工作.Below是我的代码。

users.xml

<users>
    <user>

        <username>A</username>
        <password>12345</password>
    </user>

    <user>

        <username>B</username>
        <password>12345</password>
    </user>
</users>

try.html

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
    </head>
    <body>
        <form id="frmlogin" name="frmlogin" method="post" action="Login" onsumbit="return false;">
            <p>ENTER USER NAME <input type="text" name="login_username" id="username"></p>
            <p> ENTER PASSWORD <input type="password" name="login_pass" id="password"><br><br>
                <input type="button" class="button" value="Log in" onclick="login()">
            </p>

        </form>
        <script>            
            window.login = function(e)
            {

                if (document.frmlogin.login_username.value == "") 
                {
                    alert("User name is not blank");
                    return;
                }
                else if(document.frmlogin.login_pass.value == "")
                {
                    alert("Password is not blank");
                    return;
                }

                else
                {
                    var xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function() {
                        if (this.readyState == 4 && this.status == 200) {
                        myFunction(this);
                    }
                };
                xhttp.open("GET", "users.xml", true);
                xhttp.send();
                }
                function myFunction(xml) {
                    var xmlDoc = xhttp.responseXML;

                    var ktra = true;
                    var xml = xmlDoc.childNodes[0].childNodes[1];

                    var name = xml.childNodes["username"];        
                    var pass = xml.childNodes["password"];
                    for(var i =1 ;i<xml.childNodes.length;i++){
                        if(xml.childNodes[i].nodeName =="username")
                            name = xml.childNodes[i];
                        if(xml.childNodes[i].nodeName =="password")
                            pass = xml.childNodes[i];
                    }
                    if(name.textContent == frmlogin.login_username.value && pass.textContent== frmlogin.login_pass.value)
                    {
                        ktra = true;        

                    }
                    else
                    {
                        ktra=false;
                    }

                    if(ktra == true)
                    {
                        alert("Login Successfully !!!");
                    }
                    else
                    {
                        alert("Login Failed !!!");
                    }
                }
            }
        </script>
    </body>
</html>

0 个答案:

没有答案