1

该脚本应该带来正确或不正确答案的 Javascript 警报,警报不会弹出。可能是什么问题呢?我有一种感觉,这很容易解决,但我似乎无法找到它。我已经验证了整个页面(这只是一个片段)并且它通过了验证。

   <script type="text/javascript">
    /* <![CDATA[ */
    /* ]]> */

    function scoreQuestion1(answer){
    if (answer == "a")
       window.alert("Correct Answer");
    if (answer == "b")
       window.alert("Incorrect Answer");
    if (answer == "c")
       window.alert("Incorrect Answer");
    if (answer == "d")
       window.alert("Incorrect Answer");
    }
    function scoreQuestion2(answer){
    if (answer == "a")
       window.alert("InCorrect Answer");
    if (answer == "b")
       window.alert("Incorrect Answer");
    if (answer == "c")
       window.alert("Correct Answer");
    if (answer == "d")
       window.alert("Incorrect Answer");
    }
    function scoreQuestion3(answer){
    if (answer == "a")
       window.alert("Incorrect Answer");
    if (answer == "b")
       window.alert("Correct Answer");
    if (answer == "c")
       window.alert("Incorrect Answer");
    if (answer == "d")
       window.alert("Incorrect Answer");

    function scoreQuestion4(answer){
    if (answer == "a")
       window.alert("Incorrect Answer");
    if (answer == "b")
       window.alert("Incorrect Answer");
    if (answer == "c")
       window.alert("Correct Answer");
    if (answer == "d")
       window.alert("Incorrect Answer");
     }  
    function scoreQuestion5(answer){
    if (answer == "a")
       window.alert("Incorrect Answer");
    if (answer == "b")
       window.alert("Incorrect Answer");
    if (answer == "c")
       window.alert("Incorrect Answer");
    if (answer == "d")
       window.alert("Correct Answer");
      } 


    </script>

</head>
<body>
                           <form action="" name="quiz">
                           <p><strong>1. How many natural elements are there?</strong></p><p>
                           <input type="radio" name="question1" value="a" onclick="scoreQuestion1('a')" />92<br />   <!-- correct answer-->
                           <input type="radio" name="question1" value="b" onclick="scoreQuestion1('b')" />113<br />
                           <input type="radio" name="question1" value="c" onclick="scoreQuestion1('c')" />103<br />
                           <input type="radio" name="question1" value="d" onclick="scoreQuestion1('d')" />88<br /></p>
                           <p><strong>2. If one kg of air is compressed from 1m3 to 0.5 m3, which of the following statements is true?</strong></p><p>
                           <input type="radio" name="question2" value="a" onclick="scoreQuestion2('a')" />The density is halved.<br />
                           <input type="radio" name="question2" value="b" onclick="scoreQuestion2('b')" />The mass is halved.<br />
                           <input type="radio" name="question2" value="c" onclick="scoreQuestion2('c')" />The density is doubled.<br />    <!--correct answer-->
                           <input type="radio" name="question2" value="d" onclick="scoreQuestion2('d')" />The mass is doubled.<br /></p>
                           <p><strong>3.  What is the acceleration due to gravity?</strong></p><p>
                           <input type="radio" name="question3" value="a" onclick="scoreQuestion3('a')" />980 m/s2<br />
                           <input type="radio" name="question3" value="b" onclick="scoreQuestion3('b')" />9.8 m/s2<br />   <!--correct answer-->
                           <input type="radio" name="question3" value="c" onclick="scoreQuestion3('c')" />98 m/s2<br />
                           <input type="radio" name="question3" value="d" onclick="scoreQuestion3('d')" />0.98 m/s2<br /></p>
                           <p><strong>4.  What is the SI unit of density?</strong></p><p>
                           <input type="radio" name="question4" value="a" onclick="scoreQuestion4('a')" />cm3/g<br />
                           <input type="radio" name="question4" value="b" onclick="scoreQuestion4('b')" />m3/kg<br />
                           <input type="radio" name="question4" value="c" onclick="scoreQuestion4('c')" />kg/m3<br />      <!--correct answer-->
                           <input type="radio" name="question4" value="d" onclick="scoreQuestion4('d')" />g/cm3<br /></p>
                           <p><strong>5.  Which of these has the highest density?</strong></p><p>
                           <input type="radio" name="question5" value="a" onclick="scoreQuestion5('a')" />Lead<br />
                           <input type="radio" name="question5" value="b" onclick="scoreQuestion5('b')" />Water<br />
                           <input type="radio" name="question5" value="c" onclick="scoreQuestion5('c')" />Mercury<br />
                           <input type="radio" name="question5" value="d" onclick="scoreQuestion5('d')" />Tungsten<br /></p>   <!--correct answer-->
                           </form>
4

2 回答 2

1

您永远不会关闭 scoreQuestion3 的函数,这会导致解析错误。如果你解决了这个问题,它应该可以工作。

function scoreQuestion3(answer){
if (answer == "a")
   window.alert("Incorrect Answer");
if (answer == "b")
   window.alert("Correct Answer");
if (answer == "c")
   window.alert("Incorrect Answer");
if (answer == "d")
   window.alert("Incorrect Answer");
}

jsFiddle 的工作版本

于 2013-02-12T23:29:03.803 回答
1

您的 scoreQuestion3 缺少一个结束 }。

不过,您应该学习以下几件事:

1) 适当的缩进

如果您在下降层时缩进(实际上),那么当这些错误发生时会变得更加明显。

function scoreQuestion3(answer){
  if (answer == "a")
    window.alert("Incorrect Answer");
  if (answer == "b")
    window.alert("Correct Answer");
  if (answer == "c")
    window.alert("Incorrect Answer");
  if (answer == "d")
    window.alert("Incorrect Answer");

  function scoreQuestion4()...

您可以立即看到它不正常并相应地进行调整。

在这种情况下,直接从那开始:

2) 使用 { 括号

if(answer=="a"){
  alert("Incorrect Answer");
}

尽管您可以省略它们并且 if 语句将只使用下一行,但它变得不太清楚。

3) 其他

我想这是在您的下一课中,但代码可以大大简化为:

function scoreQuestion3(answer){
  if(answer=="a"){
    alert("Correct Answer!");
  }else{
    alert("Incorrect answer I'm afraid!");
  }
}
于 2013-02-12T23:29:13.107 回答