2015-02-08 129 views
0

當我點擊它時,如何獲得按鈕評分我的工作?它應該提供一個警告框,顯示每個正確答案的所有分數。請參見下面的代碼:JavaScript - 測驗RadioButtons和表單驗證

!DOCTYPE html> 
    <html> 
     <head> 
      <script src = "quiz.js"> </script> 
     </head> 
     <body> 
      <form> 
       <fieldset> 
        <legend> Question 1 </legend> 
        He _____ it. <br> 
        <input type = "radio" value = "wrong" name = "q1" id = "q"> don't like <br> 
        <input type = "radio" value = "right" name = "q1" id = "q"> doesn't like <br> 
        <input type = "radio" value = "wrong" name = "q1" id = "q"> don't likes <br> 
       </fieldset> <br> 

       <fieldset> 
        <legend> Question 2 </legend> 
        They _____ here very often. <br> 
        <input type = "radio" value = "right" name = "q2" id = "q"> don't come <br> 
        <input type = "radio" value = "wrong" name = "q2" id = "q"> doesn't comes <br> 
        <input type = "radio" value = "wrong" name = "q2" id = "q"> don't doesn't come <br> 
       </fieldset> <br> 

       <fieldset> 
        <legend> Question 3 </legend> 
        John and Mary _____ twice last week. <br> 
        <input type = "radio" value = "wrong" name = "q3" id = "q"> comes <br> 
        <input type = "radio" value = "wrong" name = "q3" id = "q"> come <br> 
        <input type = "radio" value = "right" name = "q3" id = "q"> coming <br> 
       </fieldset> <br> 

    function validate(){ 
     var cor = 0; 
    var radios = document.getElementById('q'); 
     for(var i = 1; i < radios.length; i++){ 
      if (radios[i].checked == true && radios[i].value == "right") { 
      cor++ 
      } 
     } 
    }alert("Correct " + cor); 
+0

我要糾正UR英語第一。問題3:約翰和瑪麗上週來了兩次。我應該來,不來。我會處理你的代碼,給我一些時間 – 2015-02-08 10:01:46

+0

大聲笑..這是一個MCQ類型的問題:P這就是爲什麼我們也有一些英文錯誤! – user3545953 2015-02-08 10:09:50

+0

您想在點擊按鈕或用戶檢查最後一個單選按鈕時顯示得分 – 2015-02-08 10:10:51

回答

0

添加到您的HTML

<button id="results" onclick="display()"> 
Show me the answers! 
</button> 

的Javascript

var cor=0; 
function validate(){ 

    var radios = document.getElementById('q'); 
     for(var i = 1; i < radios.length; i++){ 
      if (radios[i].checked == true && radios[i].value == "right") { 
      cor++ 
      } 
     } 
} 
function display(){ 
    alert(cor);} 
+0

不幸的是,它不工作! – user3545953 2015-02-08 10:35:11

+0

哦忘了在onClick上添加括號 – 2015-02-08 11:20:26

+0

@ user3545953現在試用 – 2015-02-08 11:21:04