NaN出现在计算中

时间:2018-07-22 11:31:52

标签: javascript calculation

我陷于此功能multiplyUsed()中。 我想对所有带有两个复选框条件的框求和。 最后,如果我选中Quality框,则结果显示为NaN。为什么会发生呢?值将填入StaSta+框中。

function sumUpUsedCar() {
  var standartUsedLoan = Number ( document.getElementById("standartUsed").value);
  var standartPlusUsed = Number ( document.getElementById("standartPlusUsed").value);
  var prUsedCompleted = document.getElementById("prUsedCompleted");
  var prUsedDoesntCompleted = document.getElementById("prUsedDoesntCompleted");
  if (prUsedCompleted.checked) {
    document.getElementById("sumUsedVehicle").innerHTML = (standartUsedLoan * Number(0.04)) + (standartPlusUsed * Number(0.065));
  } else {
    document.getElementById("sumUsedVehicle").innerHTML = (standartUsedLoan * Number(0.03)) + (standartPlusUsed * Number(0.045));
  }
}

function multiplyUsed() {
  var qualityCompleted = document.getElementById("qualityCompleted");
  var sumUpUsedC = Number (document.getElementById("sumUsedVehicle");

  if (qualityCompleted.checked) {
    document.getElementById("sumUsedVehicle").innerHTML = (sumUpUsedC * Number(0.03));
  }
}
<form>

  <label class="showLabel5" for="usedCar"><b>Sta</b></label>
  <input class="standartUsed" type="number" min="100000" max="100000000" id="standartUsed" onchange="sumUpUsedCar()">

  <label class="showLabel6" for="usedCar2"><b>Sta + </b></label>
  <input class="standartPlusUsed" type="number" min="100000" max="100000000" id="standartPlusUsed" onchange="sumUpUsedCar()">
</form>



<div class="usedVehicle2"><b>Additional </b></div>

<label class="container2" for="yes"><b> Yes</b>
   <input type="checkbox" class="input2" name="yes" id="prUsedCompleted" onclick="prUsedSelection(), sumUpUsedCar()">
    </label>

<label class="container2" for="no"><b> No</b>
     <input type="checkbox" class="input2" name="no" id="prUsedDoesntCompleted" onclick="prUsedSelection()">
    </label>


<div class="mainbox5">


  <div class="quality"><b> Quality</b></div>

  <label class="container3" for="yes"><b> Yes</b>
   <input type="checkbox" class="input3" name="yes" id="qualityCompleted" onclick="multiplyUsed()">
  </label>

  <label class="container3" for="no"><b> No</b>
 
   <input type="checkbox" class="input3" name="no" id="qualityDoesntCompleted">
   </label>

  <div class="overall"><b>overall:</b></div>

  <table class="tableOverall">



    <tr>
      <td></td>
      <td id="sumUsedVehicle"></td>

  </table>

1 个答案:

答案 0 :(得分:2)

在这一行

document.getElementById("sumUsedVehicle").innerHTML =  (sumUpUsedCar * Number(0.03));

sumUpUsedCar是一个函数。您可能需要在sumUpUsedCar()处进行函数调用。将函数转换为数值时(按乘法运算的要求),其值为NaN


编辑后,在函数multiplyUsed中,表达式document.getElementById("sumUsedVehicle").valueundefined,因为DOM元素没有value属性。使用

var sumUpUsedC = Number (document.getElementById("sumUsedVehicle").innerHTML);

相反。