我陷于此功能multiplyUsed()
中。
我想对所有带有两个复选框条件的框求和。
最后,如果我选中Quality
框,则结果显示为NaN
。为什么会发生呢?值将填入Sta
和Sta+
框中。
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>
答案 0 :(得分:2)
在这一行
document.getElementById("sumUsedVehicle").innerHTML = (sumUpUsedCar * Number(0.03));
sumUpUsedCar
是一个函数。您可能需要在sumUpUsedCar()
处进行函数调用。将函数转换为数值时(按乘法运算的要求),其值为NaN
。
编辑后,在函数multiplyUsed
中,表达式document.getElementById("sumUsedVehicle").value
为undefined
,因为DOM元素没有value
属性。使用
var sumUpUsedC = Number (document.getElementById("sumUsedVehicle").innerHTML);
相反。