-2

我想知道我做错了什么?我正在尝试将一些数字放入 javascript 变量中并添加它们,但是当我取消注释打印值的行时,它们都是 NaN。

<div id="priceDisplayPoolHeating">8.00</div>
<div id="priceDisplayPetFee">7.00</div>
<div id="priceDisplayPropertyDamageProtection">4.00</div>
<div id="priceDisplayVacationPackageTotal">9.80</div>
function recalculateGrandTotal() {
  var alreadySetCosts = 30.00;

  var thePoolHeatingFeeRounded = parseFloat(document.getElementById("priceDisplayPoolHeating").value);
  var thePetFeeRounded = parseFloat(document.getElementById("priceDisplayPetFee").value);
  var thePropertyDamageProtectionFeeRounded = parseFloat(document.getElementById("priceDisplayPropertyDamageProtection").value);

  var theGrandTotal = alreadySetCosts + thePoolHeatingFeeRounded + thePetFeeRounded + thePropertyDamageProtectionFeeRounded;

  document.getElementById("priceDisplayVacationPackageTotal").innerHTML = theGrandTotal;
  document.write('<br/>The Vars: ' + alreadySetCosts + '<br/>' + thePoolHeatingFeeRounded + '<br/>' + thePetFeeRounded + '<br/>' + thePropertyDamageProtectionFeeRounded + '<br/>' + theGrandTotal);
}
4

2 回答 2

1

您正在尝试访问元素的值属性...

document.getElementById("priceDisplayPropertyDamageProtection").value

您需要文本内容:

document.getElementById("priceDisplayPropertyDamageProtection").textContent

我希望这有帮助吗?

于 2014-10-17T15:50:09.810 回答
1

这是因为 div 标签没有value.

<div id="priceDisplayPoolHeating" class="priceDisplay">8.00</div>

您必须使用以下代码更改 JS 行:

document.getElementById("priceDisplayPoolHeating").value

改为获取文本:

document.getElementById("priceDisplayPoolHeating").innerHTML

在调试时,您应该尝试查看诸如document.getElementById("priceDisplayPoolHeating").value. 如果你这样做了,你会看到它返回undefinedparseFloat(undefined);返回NaN

于 2014-10-17T15:51:56.067 回答