2017-02-12 67 views
2

我想從三個按鈕單選按鈕測驗中返回得分輸出。當我使用警報時,我有輸出工作,但由於測驗包含在模式中我不想要彈出窗口我只是想在模式內顯示輸出,使其看起來更乾淨我使用InnerHTML但是當我單擊我現在沒有輸出按鈕。我一直在嘗試使用JQuery來嘗試顯示結果並隱藏按鈕,但這也沒有幫助。有誰知道我可以如何將結果打印到「結果」?Javascript innerHtml測驗結果

感謝

<script> 
 
      
 
var answers = ["A","B","B"], 
 
    tot = answers.length; 
 

 
function getCheckedValue(radioName){ 
 
    var radios = document.getElementsByName(radioName); // Get radio group by-name 
 
    for(var y=0; y<radios.length; y++) 
 
     if(radios[y].checked) return radios[y].value; // return the checked value 
 
} 
 

 
function getScore(){ 
 
    var score = 0; 
 
    for (var i=0; i<tot; i++) 
 
    if(getCheckedValue("question"+i)===answers[i]) score += 1; // increment only 
 
    return score; 
 
} 
 

 
function returnScore(){ 
 
    print("Your score is "+ getScore() +"/"+ tot); 
 
} 
 
</script> 
 

 
var answers = ["A","B","B"], 
 
    tot = answers.length; 
 

 
function getCheckedValue(radioName){ 
 
    var radios = document.getElementsByName(radioName); // Get radio group by-name 
 
    for(var y=0; y<radios.length; y++) 
 
     if(radios[y].checked) return radios[y].value; // return the checked value 
 
} 
 

 
function getScore(){ 
 
    var score = 0; 
 
    for (var i=0; i<tot; i++) 
 
    if(getCheckedValue("question"+i)===answers[i]) score += 1; // increment only 
 
    return score; 
 
} 
 

 
function returnScore(){ 
 
    document.getElementById("results").innerHtml ("Well done! You scored "+ getScore() +"/"+ tot); 
 
    
 
} 
 
</script> 
 
    
 
      
   <p> You are only charged interest on the amount that is remaining at the end of the month<br> 
 
       <input type="radio" name="question0" value="A"> True </radio> 
 
       <input type="radio" name="question0" value="B"> False </radio> <br><hr> 
 
       </p> 
 
       
 
       <p>I have to pay off the balance in full every month <br><p> 
 
       <input type="radio" name="question1" value="A"> True </radio> 
 
       <input type="radio" name="question1" value="B"> False </radio> <br> <hr> 
 
       
 
       
 
       <p>If I don't make a payment my credit score will be unaffected <br></p> 
 
       <input type="radio" name="question2" value="A"> True </radio> 
 
       <input type="radio" name="question2" value="B"> False </radio> 
 
    
 

 
       <br><br> 
 
       
 
       <button type="button" class="btn btn-primary btn-xl page-scroll" onclick = "returnScore()">Results</button> 
 

 
<h4 id="results"> </h4> 
 
      

回答

2

問題是document.getElementById("results").innerHtml ("Well done! You scored "+ getScore() +"/"+ tot); innerHTML的不是一個函數,你必須賦值爲IN。

應該document.getElementById("results").innerHTML = "Well done! You scored "+ getScore() +"/"+ tot;

+0

是不是提出任何問題,但它已停止嘗試打印該頁面。 –

+0

演示:https://jsfiddle.net/m563ev87/,確保沒有錯別字,它的'innerHTML'不是'innerHtml',注意大寫HTML –

+0

令人驚歎的謝謝! –