gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:23:17 25 4
gpt4 key购买 nike

我有一个联系我们的表格,我想在将它提交给 php 之前用 javascript 检查一下。因此,当我调用函数 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>

最佳答案

除了这个错字,你的代码没问题:

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


在我的浏览器上,显示了以下错误消息。

"ReferenceError: Can't find variable: nrelementsngp"

希望这对您有所帮助。

关于Javascript for 循环似乎在第一次迭代后退出或卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24749439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com