无法更新分数

时间:2015-03-19 15:59:10

标签: javascript arrays checkbox

我试图将内容分数作为课程作业。

假设:(用户看到一个URL,然后选择分配给问题的复选框。每个问题都在一个数组中分配一个分数。)

什么工作:
注册个人支票并显示各自的分数

什么不起作用:
有人可以帮助我更新分数(当用户选中复选框或取消选中时)。

我将来假设如果我想增加问题,我将能够做到这一点,因为它是在一个数组中。 (我是对的)

(我在JS的第4周)



//Set up an array with the respective score
var code = new Array();
code["v1"] = 1;
code["v2"] = 2;
code["v3"] = 3;
code["v4"] = 5;




// As the user selects the checkbox I want to keep on adding the score and as the user unchecks I want to recalculate and display.

function getvalueScore() {
  var score = 0;

  //Get a reference to the form 
  var theForm = document.forms["contentForm"];

  //Get a reference to the score from the "ContentForm"
  var contentScore = theForm.elements["contentScore"];


  // loop through each check box 
  for (var i = 0; i < contentScore.length; i++) {
    //if the radio button is checked
    if (contentScore[i].checked) {
      // I want to calculate and keep updating the score
      score = code[contentScore[i].value];

    }
  }
  //return score 
  return score;
}


function calculateTotal() {
  //calculation for final score 
  var scoreCard = getvalueScore();

  //display the result
  var divobj = document.getElementById('totalPrice');
  divobj.style.display = 'block';
  divobj.innerHTML = "Your Content Score is " + scoreCard;

}

function hideTotal() {
  var divobj = document.getElementById('totalPrice');
  divobj.style.display = 'none';
}
&#13;
<!DOCTYPE html>
<html>

<head>
  <title>Content Score</title>
  <meta charset="utf-8">


  <script src="formcalculations.js"></script>

</head>

<body onload='hideTotal()'>
  <div id="wrap">
    <form action="" id="contentForm" onsubmit="return false;">
      <div>
        <div class="cont_order">

          Content Score</br>
          <label>Please select the issues you see on the page to calculate the content score</label>
          </br>

          <label class='radiolabel'>
            <input type="checkbox" name="contentScore" value="v1" onclick="calculateTotal()" />No content value</label>
          <br/>

          <label class='radiolabel'>
            <input type="checkbox" name="contentScore" value="v2" onclick="calculateTotal()" />Mediocre content value</label>
          <br/>

          <label class='radiolabel'>
            <input type="checkbox" name="contentScore" value="v3" onclick="calculateTotal()" />Obsolete content</label>
          <br/>

          <label class='radiolabel'>
            <input type="checkbox" name="contentScore" value="v4" onclick="calculateTotal()" />Irrelevant content</label>
          <br/>


          <br/>

          <br/>


          <div id="totalPrice"></div>


        </div>



      </div>
    </form>
  </div>
  <!--End of wrap-->

</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在您的代码中,您将最后选中的复选框分配给得分变量,因此您唯一需要做的就是将得分与以下内容相加:

score += code[contentScore[i].value];