请帮忙改一下代码,score这个变量的值一直是0,第一个代码后来被我改成了第二个,结果还是不行。[code=JScript]<html>
     <body onload = "display()">      <div id = "inform">
      </div>      <div id = "question1">
      </div>      <div id = "question2">
      </div>      <div id = "question3">
      </div>      <div id = "question4">
      </div>      <div id = "question5">
      </div>
 
      <div id = "control">
      </div>
 
      <script type="text/javascript">
         <!--         var count = ["1","2"]
         var controlNum;
         var questionNum;
         var score = 0;
         var m = 0;
         var s = 20;
         var p = 0;
         var time;
         var userName;
         var passNum;         function chooseQ(){
            var arr = new Array();
            for(var i = 0, j; i < 2; i++){
               questionNum = Math.floor(1 + Math.random() * 3);
               for(j = 0; j < arr.length; j++){
                  if(arr[j] == questionNum)
                  break;
               }
               if(j == arr.length){
                  arr[j] = questionNum;
                  controlNum = i;
                  printQ(questionNum);
               
               }
               else{
                  i--;
               }
            }
         }                function printQ(n){            switch (n){
               case 1:
                  document.getElementById("question" + count[controlNum]).innerHTML 
                  = "<form id = 'Q1' action = ''>"
                  + "<p>"
                  + "The ___ is just around the corner and you won’t miss it." 
                  + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
                  + "A.bicycle's shop" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
                  + "B.bicycle shop" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
                  + "C.bicycles shop" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
                  + "D.bicycles' shop" 
                  + "</label><br /></p></form>";  
               break;                  case 2:
                  document.getElementById("question" + count[controlNum]).innerHTML 
                  = "<form id = 'Q2' action = ''>"
                  + "<p>"
                  + "His daughter is always shy in ___ and she never dares to make a speech to ___." 
                  + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
                  + "A.the public;the public" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
                  + "B.public;the public" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
                  + "C.the public;public" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
                  + "D.public;public" 
                  + "</label><br /></p></form>";  
               break;                   case 3:
                  document.getElementById("question" + count[controlNum]).innerHTML 
                  = "<form id = 'Q3' action = ''>"
                  + "<p>"
                  + "Rose was wild with joy ___ the result of the examination." 
                  + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
                  + "A.to" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
                  + "B.at" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
                  + "C.by" 
                  + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
                  + "D.as" 
                  + "</label><br /></p></form>";
               break;   
   
            }
         }   
  
       
         function timeControl(){
            if(p == 0){
               if(s == 0){
                  if(m == 0){
                     p = 1;
                     return confirm("5 minutes have passed." + "\n" + "You should do the quiz again.");
                  }
                  else{
                     m = m - 1;
                     s = 60;
                  }
               }
               s = s - 1;
               var printM = m;
               var printS = s;
               if(m < 10){
                  printM = "0" + m;
               }
               if(s < 10){
                  printS = "0" + s;
               }
               document.getElementById("inform").innerHTML = "00:" + printM + ":" + printS;
            }
            time = setTimeout("timeControl()",1000);
         }         function stopTime(){
            clearTimeout(time);
         }         function display(){
            document.getElementById("control").innerHTML
            = "<form name = 'information' action = ''><p><label>"
            + "Enter you name here:"
            + "<br /><input name = 'userName' type = 'text' size = '40' />"
            + "</label></p><p><label>"
            + "Enter a number here which is the passed score threshold:"
            + "<br /><input name = 'passNum' type = 'text' size = '40' />"
            + "</label></p><p>"
            + "Click this button to start you quiz."
            + "<br /><input name = 'startButton' type = 'button' value = 'start'"
            + " onclick = 'displayConfirm()'/>"
            + "</p></form>";
         }            
         function isClick(){
            document.getElementById("control").innerHTML
            = "<form name = 'information' action = ''>"
            + "<input id = 'submitButton' type = 'button' value = 'submit'"
            + "onclick = 'stopTime();isPass(questionNum);' />"
            + "</form>";            
         }                         function displayConfirm(){
            var message;
            userName = document.information.userName.value;
            passNum = document.information.passNum.value;
            if((userName == "") && (passNum == "")){
               window.alert("error" + "\nYou should enter you name and pass number you want");
            }
            else{
               if(userName == ""){
                  window.alert("error" + "\nYou should enter you name in the text");
               }
               else{
                  if(passNum == ""){
                     window.alert("error" + "\nYou shoule enter a number in the text");
                  }
                  else{
                     message = confirm("Hello, " + userName + "\nWelcome to do English quiz"
                                       + "\nYou have set " + passNum + " to be your pass score");
                  }
               }
            }            if(message == true){
               timeControl();
               chooseQ();
               isClick();
            }
            else{
               display();
            }           
        }       
 
        function isPass(n){  
           switch(n){
              case 1:
                 if(Q1.radiobutton.checked){
                   if(Q1.radiobutton[1].checked)
                  score += 20;}
              break;
                 
              case 2:
               
                   if(Q2.radiobutton.checked){
                   if(Q2.radiobutton[1].checked)
                  score += 20;}
              break;              case 3:
                if(Q3.radiobutton.checked){
                   if(Q3.radiobutton[1].checked)
                  score += 20;}
              break;
           }           if(score >= passNum){
              document.getElementById("inform").innerHTML
              = "PASS"
              + "<br />"
              + "Your score is " + score;
           }
           else{
              document.getElementById("inform").innerHTML
              = "FAIL"
              + "<br />"
              + "Your score is " + score;        
           }
        }         //-->
      </script>
      
   </body>
</html>

解决方案 »

  1.   

    <html>
      <body onload = "display()">  <div id = "inform">
      </div>  <div id = "question1">
      </div>  <div id = "question2">
      </div>  <div id = "question3">
      </div>  <div id = "question4">
      </div>  <div id = "question5">
      </div>
      
      <div id = "control">
      </div>
      
      <script type="text/javascript">
      <!--  var count = ["1","2"]
      var controlNum;
      var questionNum;
      var score = 0;
      var m = 0;
      var s = 20;
      var p = 0;
      var time;
      var userName;
      var passNum;  function chooseQ(){
      var arr = new Array();
      for(var i = 0, j; i < 2; i++){
      questionNum = Math.floor(1 + Math.random() * 3);
      for(j = 0; j < arr.length; j++){
      if(arr[j] == questionNum)
      break;
      }
      if(j == arr.length){
      arr[j] = questionNum;
      controlNum = i;
      printQ(questionNum);
        
      }
      else{
      i--;
      }
      }
      }     function printQ(n){  switch (n){
      case 1:
      document.getElementById("question" + count[controlNum]).innerHTML  
      = "<form id = 'Q1' action = ''>"
      + "<p>"
      + "The ___ is just around the corner and you won’t miss it."  
      + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
      + "A.bicycle's shop"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
      + "B.bicycle shop"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
      + "C.bicycles shop"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
      + "D.bicycles' shop"  
      + "</label><br /></p></form>";   
      break;     case 2:
      document.getElementById("question" + count[controlNum]).innerHTML  
      = "<form id = 'Q2' action = ''>"
      + "<p>"
      + "His daughter is always shy in ___ and she never dares to make a speech to ___."  
      + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
      + "A.the public;the public"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
      + "B.public;the public"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
      + "C.the public;public"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
      + "D.public;public"  
      + "</label><br /></p></form>";   
      break;     case 3:
      document.getElementById("question" + count[controlNum]).innerHTML  
      = "<form id = 'Q3' action = ''>"
      + "<p>"
      + "Rose was wild with joy ___ the result of the examination."  
      + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
      + "A.to"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
      + "B.at"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
      + "C.by"  
      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
      + "D.as"  
      + "</label><br /></p></form>";
      break;   
        
      }
      }   
       
        
      function timeControl(){
      if(p == 0){
      if(s == 0){
      if(m == 0){
      p = 1;
      return confirm("5 minutes have passed." + "\n" + "You should do the quiz again.");
      }
      else{
      m = m - 1;
      s = 60;
      }
      }
      s = s - 1;
      var printM = m;
      var printS = s;
      if(m < 10){
      printM = "0" + m;
      }
      if(s < 10){
      printS = "0" + s;
      }
      document.getElementById("inform").innerHTML = "00:" + printM + ":" + printS;
      }
      time = setTimeout("timeControl()",1000);
      }  function stopTime(){
      clearTimeout(time);
      }  function display(){
      document.getElementById("control").innerHTML
      = "<form name = 'information' action = ''><p><label>"
      + "Enter you name here:"
      + "<br /><input name = 'userName' type = 'text' size = '40' />"
      + "</label></p><p><label>"
      + "Enter a number here which is the passed score threshold:"
      + "<br /><input name = 'passNum' type = 'text' size = '40' />"
      + "</label></p><p>"
      + "Click this button to start you quiz."
      + "<br /><input name = 'startButton' type = 'button' value = 'start'"
      + " onclick = 'displayConfirm()'/>"
      + "</p></form>";
      }    
      function isClick(){
      document.getElementById("control").innerHTML
      = "<form name = 'information' action = ''>"
      + "<input id = 'submitButton' type = 'button' value = 'submit'"
      + "onclick = 'stopTime();isPass(questionNum);' />"
      + "</form>";   
      }     function displayConfirm(){
      var message;
      userName = document.information.userName.value;
      passNum = document.information.passNum.value;
      if((userName == "") && (passNum == "")){
      window.alert("error" + "\nYou should enter you name and pass number you want");
      }
      else{
      if(userName == ""){
      window.alert("error" + "\nYou should enter you name in the text");
      }
      else{
      if(passNum == ""){
      window.alert("error" + "\nYou shoule enter a number in the text");
      }
      else{
      message = confirm("Hello, " + userName + "\nWelcome to do English quiz"
      + "\nYou have set " + passNum + " to be your pass score");
      }
      }
      }  if(message == true){
      timeControl();
      chooseQ();
      isClick();
      }
      else{
      display();
      }   
      }   
     
      function isPass(n){   
      switch(n){
      case 1:
      if(Q1.radiobutton.checked){
      if(Q1.radiobutton[1].checked)
      score += 20;}
      break;
        
      case 2:
        
      if(Q2.radiobutton.checked){
      if(Q2.radiobutton[1].checked)
      score += 20;}
      break;  case 3:
      if(Q3.radiobutton.checked){
      if(Q3.radiobutton[1].checked)
      score += 20;}
      break;
      }  if(score >= passNum){
      document.getElementById("inform").innerHTML
      = "PASS"
      + "<br />"
      + "Your score is " + score;
      }
      else{
      document.getElementById("inform").innerHTML
      = "FAIL"
      + "<br />"
      + "Your score is " + score;   
      }
      }  //-->
      </script>
        
      </body>
    </html>
      

  2.   

    <html>   <body onload = "display()">      <div id = "inform">
          </div>      <div id = "question1">
          </div>      <div id = "question2">
          </div>      <div id = "question3">
          </div>
     
          <div id = "control">
          </div>
     
          <script type="text/javascript">
             <!--         var count = ["1","2"]
             var controlNum;
             var questionNum;
             var score = 0;
             var m = 0;
             var s = 20;
             var p = 0;
             var time;
             var userName;
             var passNum;         function chooseQ(){
                var arr = new Array();
                for(var i = 0, j; i < 2; i++){
                   questionNum = Math.floor(1 + Math.random() * 3);
                   for(j = 0; j < arr.length; j++){
                      if(arr[j] == questionNum)
                      break;
                   }
                   if(j == arr.length){
                      arr[j] = questionNum;
                      controlNum = i;
                      printQ(questionNum);
                      checkAnswer(questionNum);
                   }
                   else{
                      i--;
                   }
                }
             }                function printQ(n){            switch (n){
                   case 1:
                      document.getElementById("question" + count[controlNum]).innerHTML 
                      = "<form id = 'Q1' action = 'JavaScript:isCheck()'>"
                      + "<p>"
                      + "The ___ is just around the corner and you won’t miss it." 
                      + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
                      + "A.bicycle's shop" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
                      + "B.bicycle shop" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
                      + "C.bicycles shop" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
                      + "D.bicycles' shop" 
                      + "</label><br /></p></form>";  
                   break;                  case 2:
                      document.getElementById("question" + count[controlNum]).innerHTML 
                      = "<form id = 'Q2' action = 'JavaScript:isCheck()'>"
                      + "<p>"
                      + "His daughter is always shy in ___ and she never dares to make a speech to ___." 
                      + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
                      + "A.the public;the public" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
                      + "B.public;the public" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
                      + "C.the public;public" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
                      + "D.public;public" 
                      + "</label><br /></p></form>";  
                   break;                   case 3:
                      document.getElementById("question" + count[controlNum]).innerHTML 
                      = "<form id = 'Q3' action = 'JavaScript:isCheck()'>"
                      + "<p>"
                      + "Rose was wild with joy ___ the result of the examination." 
                      + "<br /><label><input type = 'radio' name = 'radiobutton' value = 'A'>"
                      + "A.to" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'B'>"
                      + "B.at" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'C'>"
                      + "C.by" 
                      + "</label><br /><label><input type = 'radio' name = 'radiobutton' value = 'D'>"
                      + "D.as" 
                      + "</label><br /></p></form>";
                   break;   
                }
             }   
      
             function checkAnswer(n){
                switch (n){
                   case 1:
                      if(Q1.radiobutton[1].checked)
                      score += 20;
                   break;               case 2:
                      if(Q2.radiobutton[1].checked)
                      score += 20;
                   break;               case 3:
                      if(Q3.radiobutton[1].checked)
                      score += 20;
                   break;
       
                }
             }         function timeControl(){
                if(p == 0){
                   if(s == 0){
                      if(m == 0){
                         p = 1;
                         return confirm("5 minutes have passed." + "\n" + "You should do the quiz again.");
                      }
                      else{
                         m = m - 1;
                         s = 60;
                      }
                   }
                   s = s - 1;
                   var printM = m;
                   var printS = s;
                   if(m < 10){
                      printM = "0" + m;
                   }
                   if(s < 10){
                      printS = "0" + s;
                   }
                   document.getElementById("inform").innerHTML = "00:" + printM + ":" + printS;
                }
                time = setTimeout("timeControl()",1000);
             }         function stopTime(){
                clearTimeout(time);
             }         function display(){
                document.getElementById("control").innerHTML
                = "<form name = 'information' action = ''><p><label>"
                + "Enter you name here:"
                + "<br /><input name = 'userName' type = 'text' size = '40' />"
                + "</label></p><p><label>"
                + "Enter a number here which is the passed score threshold:"
                + "<br /><input name = 'passNum' type = 'text' size = '40' />"
                + "</label></p><p>"
                + "Click this button to start you quiz."
                + "<br /><input name = 'startButton' type = 'button' value = 'start'"
                + " onclick = 'displayConfirm()'/>"
                + "</p></form>";
             }            
             function isClick(){
                document.getElementById("control").innerHTML
                = "<form name = 'information' action = ''>"
                + "<input id = 'submitButton' type = 'button' value = 'submit'"
                + "onclick = 'stopTime();isPass();' />"
                + "</form>";            
             }                         function displayConfirm(){
                var message;
                userName = document.information.userName.value;
                passNum = document.information.passNum.value;
                if((userName == "") && (passNum == "")){
                   window.alert("error" + "\nYou should enter you name and pass number you want");
                }
                else{
                   if(userName == ""){
                      window.alert("error" + "\nYou should enter you name in the text");
                   }
                   else{
                      if(passNum == ""){
                         window.alert("error" + "\nYou shoule enter a number in the text");
                      }
                      else{
                         message = confirm("Hello, " + userName + "\nWelcome to do English quiz"
                                           + "\nYou have set " + passNum + " to be your pass score");
                      }
                   }
                }            if(message == true){
                   timeControl();
                   chooseQ();
                   isClick();
                }
                else{
                   display();
                }           
            }               function isCheck(){
               if(Q + "questionNum".radiobutton.checked)
                  checkAnswer(questionNum);
            }
                  
     
            function isPass(){      
               if(score >= passNum){
                  document.getElementById("inform").innerHTML
                  = "PASS"
                  + "<br />"
                  + "Your score is " + score;
               }
               else{
                  document.getElementById("inform").innerHTML
                  = "FAIL"
                  + "<br />"
                  + "Your score is " + score;        
               }
            }         //-->
          </script>
          
       </body>
    </html>
      

  3.   

    我看了你的代码,你每次在为选出答案的时候就调用了checkAnswer()函数,也就是说调用的顺序错了,点击submit的时候应该调用,而不是之前