0

我有一个 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导致此问题。其他一切工作正常。

4

2 回答 2

2

您可能想使用布尔&&运算符,而不是按位运算&符。

result = result && ele.checked;

这也取决于 的先前值result。我不完全确定这是您现阶段的问题。

更新

我无法重现该问题。见http://jsfiddle.net/QA26Q/

我唯一的另一个想法是,您有多个具有相同 ID 的单选按钮,如本示例所示 - http://jsfiddle.net/QA26Q/1/

于 2013-09-27T03:48:46.757 回答
0

有问题。问题在于 HTML 而不是 JavaScript。我错误地将name所有输入的字段设置为相同,即Answer。因此,所有replaceable divs 中的所有单选按钮都属于同一组。所以当我设置的Answer2单选replaceable2按钮replaceable1被重置时。由于不可见,我无法跟踪此错误。所以简单地我将html更改为

<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="Answers1" title="True" onclick="setAnswered();"/>True<br/>
        <input type="radio" name="Answers1" 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="Answers2" title="True" onclick="setAnswered();" />True<br/>
        <input id="Answer2" type="radio" name="Answers2" 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="Answers3" title="True" onclick="setAnswered();"/>Morning<br/>
        <input id="notAnswer2" type="checkbox" name="Answers3" title="False" onclick="setAnswered();" />Evening<br/>
        <input id="Answer3" type="checkbox" name="Answers3" 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="Answers4" title="False" onclick="setAnswered();" />Launch<br/>
        <input id="Answer5" type="checkbox" name="Answers4" title="False" onclick="setAnswered();" />Break Fast<br/>
        <input id="Answer6" type="checkbox" name="Answers4" 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>

它工作正常。

于 2013-10-01T07:05:45.493 回答