[英]for loop stops at first iteration
EDIT Thank you all for your assistance. 编辑谢谢大家的帮助。 I have made the modifications to the script, with try and catch (err), however, i still do not get the alert when the code is run.
我已经使用try and catch(err)对脚本进行了修改,但是,当代码运行时,我仍然没有收到警报。 I've also replaced "studentInfo[i].getElementsByTagName("id")[i].childNodes[i].nodeValue" with "studentInfo[i].getElementsByTagName("id")[0].childNodes[0].nodeValue" as well as all similar references, except now, it won't even return the first loop.
我还将“ studentInfo [i] .getElementsByTagName(“ id”)[i] .childNodes [i] .nodeValue”替换为“ studentInfo [i] .getElementsByTagName(“ id”)[0] .childNodes [0]。 nodeValue”以及所有类似的引用,但现在除外,它甚至不会返回第一个循环。 It seems to be exiting the function before it hits "catch" for some reason.I've marked the changes in bold.
由于某种原因,它似乎在退出“ catch”之前就退出了。我已将更改标记为粗体。
I know this has been posted quite a bit on this site, but none of the answers seem to quite be able to help me. 我知道这已经在该网站上发布了很多,但是似乎没有任何答案可以帮助我。 I have a for loop that stops iterating after the first loop.
我有一个for循环,在第一个循环后停止迭代。 The data from the first loop is correct, but I need it to keep looping through.
来自第一个循环的数据是正确的,但是我需要它来不断循环。 I've used a couple different lint tools and they say my code is valid, so I must be forcing it to exit the loop some how.
我使用了几个不同的lint工具,他们说我的代码有效,因此我必须强制其退出循环。 Someone help me figure out what I'm doing wrong, please.
请有人帮我弄清楚我在做什么错。
<html>
<head>
<title>Tardy Reporting</title>
<script type="text/javascript" src="students.js">
</script>
</head>
<body>
<h1>Scan in Student ID</h1>
<form method="POST" name="idForm" id="idForm" />
<input type="text" name="idNumber" id="idNumber"/>
<input type="button" name="Search" value="Search" onClick="getId(document.idForm.idNumber.value);" />
</form>
<br></br>
<div id="div1"></div>
<p>
</body>
</html>
var ajxObj;
if(window.XMLHttpRequest){
ajxObj = new XMLHttpRequest();
}
else{
ajxObj = new ActiveXObject('Microsoft.XMLHTTP');
}
ajxObj.open("GET","studentbase.xml",false);
ajxObj.send();
xmlData = ajxObj.responseXML;
var studentInfo = xmlData.getElementsByTagName("student");
function getId(studentId) {
**try{**
for(var i = 0; i < studentInfo.length; i++) {
if(studentId == **studentInfo[i].getElementsByTagName("id")[0].childNodes[0].nodeValue || studentId === studentInfo[i].getElementsByTagName("name")[0].childNodes[0].nodeValue**){
document.getElementById('div1').innerHTML=(studentInfo[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
}
else {
document.getElementById('div1').innerHTML="Error: Not Found"
}
}
**}catch (err){
alert(err.ToString());
}**
} }
<?xml version="1.0" encoding="UTF-8" ?>
<thebase>
<student>
<id>50011234</id>
<name>Mike Simpson</name>
<grade>n/a</grade>
<teacher>George Washington</teacher>
<tardies>0</tardies>
</student>
<student>
<id>50012345</id>
<name>Greg Pollard</name>
<grade>n/a</grade>
<teacher>Darth Vadar</teacher>
<tardies>0</tardies>
</student>
<student>
<id>50013456</id>
<name>Jason Vigil</name>
<grade>n/a</grade>
<teacher>Obi Wan Kenobi </teacher>
<tardies>0</tardies>
</student>
</thebase>
I suspect your code is throwing an error and you are not aware of this. 我怀疑您的代码抛出错误,并且您不知道这一点。 I suspect the reference "studentInfo[i].getElementsByTagName("id")[i].childNodes[i].nodeValue" should be "studentInfo[i].getElementsByTagName("id")[0].childNodes[0].nodeValue".
我怀疑引用“ studentInfo [i] .getElementsByTagName(“ id”)[i] .childNodes [i] .nodeValue”应为“ studentInfo [i] .getElementsByTagName(“ id”)[0] .childNodes [0]。 nodeValue”。
Try putting a "try...catch" around the "for" loop, like so: 尝试在“ for”循环中放置“ try ... catch”,如下所示:
function getId(studentId) {
try {
for(var i = 0; i < studentInfo.length; i++) {
if (studentId == studentInfo[i].getElementsByTagName("id")[i].childNodes[i].nodeValue || studentId === studentInfo[i].getElementsByTagName("name")[i].childNodes[i].nodeValue){
document.getElementById('div1').innerHTML=(studentInfo[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
}
else {
document.getElementById('div1').innerHTML="Error: Not Found"
}
}
} catch (err) {
alert(err.ToString());
}
}
if
语句中必须有三个等号(即“ ===”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.