Javascript innerHtml测验结果

时间:2017-02-12 17:43:50

标签: javascript jquery html

我想从三个按钮单选按钮测验中返回乐谱的输出。当我使用警报时,我的输出正常工作,但由于测验包含在模态中我不想要弹出窗口我只想在模态中显示输出以使其看起来更清晰我使用InnerHTML但是当我点击按钮时,我现在没有输出。我一直在尝试使用JQuery来尝试显示结果并隐藏按钮,但这也没有帮助。有谁知道如何将结果打印到"结果"?

由于



<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>
 
                
&#13;
                <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>
                
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

问题在于document.getElementById("results").innerHtml ("Well done! You scored "+ getScore() +"/"+ tot); innerHTML不是函数,你必须为in赋值。

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