我有一个 HTML 页面,其中有很多部分,其中包含由RadioButtons
和组成的输入CheckBox
。其中第一个是这样的:
<div style="margin-top: 30px; height: 270px;">
<input id="Answer1" type="radio" name="Answers" title="True" onclick="setAnswered();"/>True<br/>
<input type="radio" name="Answers" title="False" onclick="setAnswered();" />False
</div>
<div id="replaceAble1_a" style="display: none;">Answer1</div>
在最终输入之后,我正在寻找每个输入并调查是否检查了正确的答案。这是执行此工作的脚本部分:
var ansList = document.getElementById(thisFrame + "_a").innerHTML.split(",");
for (var k = 0; k < ansList.length; k++) {
var ele = document.getElementById(ansList[k]);
result = result & ele.checked ;
}
thisFrame
这个迭代在哪里replaceAble1
。调试后很明显,我在ansList[k]
ie中获得了正确的元素 id Answer1
。现在的问题是,每次Answer1
检查是否都在ele.checked
返回false
。可能是什么原因?
更新:
我认为最好把完整的代码。所以这里是:
<div id="replaceAble1">
<span style="font-size: 14pt ;margin-top: 5px;">Q
<span id ="question1">Do you live in Pakistan?</span>
</span>
<div style="margin-top: 30px; height: 270px;">
<input id="Answer1" type="radio" name="Answers" title="True" onclick="setAnswered();"/>True<br/>
<input type="radio" name="Answers" title="False" onclick="setAnswered();" />False
</div>
<div id="replaceAble1_a" style="display: none;">Answer1</div>
<div id="replaceAble1_n" style="display: none;">null</div>
</div>
<div id="replaceAble2" style="display: none;">
<span style="font-size: 14pt ;margin-top: 5px;">Q.
<span id ="question2">Do you go for cycling daily?</span>
</span>
<div style="margin-top: 30px; height: 270px;">
<input type="radio" name="Answers" title="True" onclick="setAnswered();" />True<br/>
<input id="Answer2" type="radio" name="Answers" title="False" onclick="setAnswered();" />False
</div>
<div id="replaceAble2_a" style="display: none;">Answer2</div>
<div id="replaceAble2_n" style="display: none;">null</div>
</div>
<div id="replaceAble3" style="display: none;">
<span style="font-size: 14pt ;margin-top: 5px;">Q
<span id ="question3">When Do you Sleep?</span>
</span>
<div style="margin-top: 30px; height: 270px;">
<input id="notAnswer1" type="checkbox" name="Answers" title="True" onclick="setAnswered();"/>Morning<br/>
<input id="notAnswer2" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Evening<br/>
<input id="Answer3" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Night
</div>
<div id="replaceAble3_a" style="display: none;">Answer3</div>
<div id="replaceAble3_n" style="display: none;">notAnswer1,notAnswer2</div>
</div>
<div id="replaceAble4" style="display: none;">
<span style="font-size: 14pt ;margin-top: 5px;">Q
<span id ="question4">What Do you Eat?</span>
</span>
<div style="margin-top: 30px; height: 270px;">
<input id="Answer4" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Launch<br/>
<input id="Answer5" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Break Fast<br/>
<input id="Answer6" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Dinner<br/>
</div>
<div id="replaceAble4_a" style="display: none;">Answer4,Answer5,Answer6</div>
<div id="replaceAble4_n" style="display: none;">null</div>
</div>
脚本是:
var i = 0;
var answered = false;
var score = 0;
var unitScore = 5;
var totalScore = 20;
var frameList = ["replaceAble1", "replaceAble2", "replaceAble3", "replaceAble4"];
function setAnswered() {
answered = true;
}
function displayAnswer(isTimeUp) {
var Message;
var j;
if (isTimeUp) {
Message = "Times Up";
} else {
Message = "Quiz End";
}
for (j = 0; j < frameList.length; j++) {
var result = true;
var thisFrame = frameList[j];
var ansList = document.getElementById(thisFrame + "_a").innerHTML.split(",");
for (var k = 0; k < ansList.length; k++) {
var ele = document.getElementById(ansList[k]);
result = result & ele.checked ;
}
ansList = document.getElementById(thisFrame + "_n").innerHTML.split(",");
for (var k = 0; k < ansList.length; k++) {
if (ansList[k] !== "null")
result = result & (!document.getElementById(ansList[k]).checked);
}
if (result) {
score = Number(score) + Number(unitScore);
}
}
document.getElementById("QuestionPan").style.display = "none";
document.getElementById("resultPan").style.display = "block";
document.getElementById("message").innerHTML = Message;
document.getElementById("totalMarks").innerHTML = totalScore;
document.getElementById("score").innerHTML = score;
var timetaken = count - document.getElementById("time").innerHTML;
document.getElementById("timetaken").innerHTML = timetaken + " minutes";
}
function submition() {
if (!answered) {
alert("Please select at least one Answer for this question.");
return;
}
if (i === (frameList.length - 1)) {
displayAnswer(false);
return;
}
document.getElementById(frameList[i++]).style.display = "none";
document.getElementById(frameList[i]).style.display = "block";
document.getElementById("counter").innerHTML = (Number(i) + 1) + '/' + frameList.length;
answered = false;
}
只有第一个答案,即Answer1
导致此问题。其他一切工作正常。