Javascript for循环似乎在第一次迭代后退出或卡住

时间:2014-07-15 03:34:20

标签: javascript for-loop

我有一个联系我们表格,我想用javascript检查,然后再提交给php。所以当我调用函数verify()时,通过在整个代码中放置alert(“hello”),我确定它在第二次循环的第一次迭代后退出。有谁知道为什么?

继承我的html表单代码:

<div id="errortext">
</div>
<form id="contact" method="post" action="contact.php">
    <h2>Contact Us</h2>
    <h1>Fields marked with a * are required</h1>
    <p>Full Name*</p>
    <input type="text" class="single" placeholder="Full Name" name="name" id="name" /><br />
    <p>E-Mail Address*</p>
    <input type="text" class="single" placeholder="E-Mail Address" name="email" id="email" /><br />
    <p>Phone Number</p>
    <input type="text" class="single" placeholder="Phone Number" name="number" id="number" /><br />
    <p>Subject</p>
    <input type="text" class="single" placeholder="Subject" name="subject" id="subject" /><br />
    <p>Message*</p>
    <textarea placeholder="Message" name"message" id="message"></textarea><br />
    <input type="button" id="submitb" onclick="verify()" value="SEND" />
</form>

并且继承了我的javascript代码:

<script type="text/javascript">
    function verify()
    {
        var rgood = true, nrgood = true;
        var name = document.getElementById("name");
        var email = document.getElementById("email");
        var phone = document.getElementById("number");
        var subject = document.getElementById("subject");
        var message = document.getElementById("message");
        var error = document.getElementById("errortext");

        var relements = new Array(name, email,  message);
        var nrelements = new Array(phone, subject);

        var relementsng = new Array;
        var nrelementsng = new Array;
        var relementsngp = new Array;
        var nrelementsng = new Array;

        for (var x = 0; x < relements.length; x++)
        {
            if (relements[x].value == "")
            {
                rgood = false;
                relementsng.push(relements[x]);
                relementsngp.push(relements[x].placeholder);
            }
        }

        for (var y = 0; y < nrelements.length; y++)
        {
            if (nrelements[y].value == "")
            {
                nrgood = false;
                nrelementsng.push(nrelements[y]);
                nrelementsngp.push(nrelements[y].placeholder);
            }
        }
        alert("hello");
        if (!rgood)
        {
            error.textContent = "All required fields are not filled out! These fields must be filled out: " + nrelementsngp.toString() + "!";
        }
    }
</script>

2 个答案:

答案 0 :(得分:1)

除了这个错字之外,你的代码还可以:

line23: nrelementsngp.push(nrelements[x].placeholder);
                    ^
line31: nrelementsngp.push(nrelements[y].placeholder);
                    ^

<小时/> 在我的浏览器中,显示了以下错误消息。

"ReferenceError: Can't find variable: nrelementsngp"

希望这有帮助。

答案 1 :(得分:1)

    var relementsng = new Array;
    var nrelementsng = new Array;
    var relementsngp = new Array;
    var nrelementsng = new Array; // <<---- Change this line to

    var nrelementsngp = new Array;

由于

for (var y = 0; y < nrelements.length; y++){
    if (nrelements[y].value == ""){
        nrgood = false;
        nrelementsng.push(nrelements[y]);
        nrelementsngp.push(nrelements[y].placeholder); // This place is causing your script to crash
    }
}

标识符nrelementsngp未声明。