Javascript只输出if else if中的默认值

时间:2014-04-21 04:33:13

标签: javascript

我正在使用html和javascript为网站撰写测验。 我的问题是,当我运行我的测验以查看我的结果时,只显示默认值。无论我如何回答测验问题,都会发生这种情况。有谁看到我能做些什么来解决它?每次回答一个问题时,应该更新不同结果的总数(如果问题答案对应于结果,则加1,否则不向结果总计加1)。我认为我的if语句需要改变。我玩过它,但似乎无法搞清楚。

<!DOCTYPE html>
<html>
<head>
<title>Quiz</title>
        <style type = "text/css">
            em  { font-weight: bold;
                  color: black; }
            p   { font-size: 12pt; 
                  font-family: times new roman, sans-serif; color: black; }
            b   { font-size: 13pt; 
                  font-family: times new roman, sans-serif; color: black; }
        </style>
<script language="Javascript">

function process()
{
    var A = 0;
    var B = 0;
    var C = 0;
    var D = 0;

    var f = document.f;
    var i = 0;

    for (i = 0; i < f.q1.length; i++) if (f.q1[i].checked) value = f.q1[i].value;
        if (value == "1") { } /*corresponds to the answer being chosen, i.e. first answer*/
        if (value == "2") { C++; A++; B++; }
        if (value == "3") { C++; D++; A++; B++; }

    for (i = 0; i < f.q2.length; i++) if (f.q2[i].checked) value = f.q2[i].value;
        if (value == "1") { C++; D++; A++; }
        if (value == "2") { D++; A++; B++; }
        if (value == "3") { C++; A++; B++; }
        if (value == "4") { C++; D++; A++; B++; }

    for (i = 0; i < f.q3.length; i++) if (f.q3[i].checked) value = f.q3[i].value;
        if (value == "1") { D++; }
        if (value == "2") { }

    for (i = 0; i < f.q4.length; i++) if (f.q4[i].checked) value = f.q4[i].value;
        if (value == "1") { A++; }
        if (value == "2") { A++; B++; }
        if (value == "3") { C++; D++; A++; B++; }

    /*same format for all other questions*/

    var out = "A"; /*default value*/
    i = "A"; /*default value*/

    if (C > i) { out ="C"; i = "C"; } 
    if (B > i) { out ="B"; i = "B"; }
    if (D > i) { out ="D"; i = "D"; } 
    window.alert ("Based on your answers, your result is " + i + " !!"); } /*right now, i always outputs as A*/

    </script>
</head>

<body>
<?php include ('menu.html'); ?>
<? include ("file.php"); ?>
<br><p>Answer the questions below</p> <br>

<form name="f" method= "post" action= "file.php">

<p><b>1. Question1 Text?<br></b>
    <input type="radio" name="q1" id="ans" value="1">Q1 Ans1<br>
    <input type="radio" name="q1" id="ans" value="2">Q1 Ans2<br>
    <input type="radio" name="q1" id="ans" value="3">Q1 Ans3<br><br>

<b>2. Question2 Text?<br></b>
    <input type="radio" name="q2" id="ans" value="1">Q2 Ans1<br>        
    <input type="radio" name="q2" id="ans" value="2">Q2 Ans2<br>
    <input type="radio" name="q2" id="ans" value="3">Q2 Ans3<br>
    <input type="radio" name="q2" id="ans" value="4">Q2 Ans4<br><br>

<b>3. Question3 Text?<br></b>
    <input type="radio" name="q3" id="ans" value="1">Q3 Ans1<br>
    <input type="radio" name="q3" id="ans" value="2">Q3 Ans2<br><br>

<b>4. Question4 Text?<br></b>
    <input type="radio" name="q4" id="ans" value="1">Q4 Ans1<br>        
    <input type="radio" name="q4" id="ans" value="2">Q4 Ans2<br>
    <input type="radio" name="q4" id="ans" value="3">Q4 Ans3<br><br>


/*more questions with same format*/


<p>Thanks for taking the quiz.<p><br>
<input type="button" value="Result" onclick="process();"><br><br>
</form>

<div class="footer"><?php include('myfooter.html');?></div>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我所看到的问题是,在下面的示例for循环中,错误是只有第一个if语句将包含在for循环中:

所以,下面的代码是:

for (i = 0; i < f.q1.length; i++) if (f.q1[i].checked) value = f.q1[i].value;
    if (value == "1") { } /*corresponds to the answer being chosen, i.e. first answer*/
    if (value == "2") { C++; A++; B++; }
    if (value == "3") { C++; D++; F++; B++; }

实际上是这样做的:

for (i = 0; i < f.q1.length; i++) {
    if (f.q1[i].checked) {
        value = f.q1[i].value;
    }
}

if (value == "1")  { } /*corresponds to the answer being chosen, i.e. first answer*/
if (value == "2") { 
    C++; A++; B++;
}
if (value == "3") { 
    C++; D++; F++; B++; 
}

请调整代码,以便所有if语句都在for-loop的范围内:

for (i = 0; i < f.q1.length; i++) {
    if (f.q1[i].checked) {
        value = f.q1[i].value;
    }
    if (value == "1") { } /*corresponds to the answer being chosen, i.e. first answer*/
    if (value == "2") { C++; A++; B++; }
    if (value == "3") { C++; D++; F++; B++; }
}

一般来说,由于上述原因,即使是单一陈述,也要忽略{}

另外,下面的代码:

var out = "A"; /*default value*/
i = "A"; /*default value*/

最终会退出&#39;和&#39;我&#39;接收字符串的变量&#34; A&#34;。你希望他们成为:

var out = "A"; /*default value*/
i = A; /*default value*/

并且还要更改下面的其余任务语句以消除&#34;&#34;为了让他们接收值而不是字符串:

var out = "A"; 
i = A; 

if (C > i) { 
    out = "C"; i = C; 
}else if (B > i) { 
    out = "B"; i = B; 
}else if (D > i) { 
    out = "D"; i = D; 
} 
alert ("Based on your answers, your result is " + out + " !!");

还有一件事,第一个for循环包括一个fauty语句&#34; F ++&#34;,它应该是&#34; A ++&#34;代替:

if (value == "3") { C++; D++; A++; B++; }

请参阅下面的工作代码:

JSfiddle