在一个表单中,onkeypress,我调用这个函数:
function calculate() {
var subtotal = parseInt(document.form1.product1.value) * product1 +
parseInt(document.form1.product2.value) * product2 +
parseInt(document.form1.product3.value) * product3;
alert(typeof subtotal);
alert(subtotal);
var total = subtotal + parseInt(document.form1.shipping.value);
document.form1.subtotal.value = subtotal;
document.form1.total.value = total;
}
当我提醒小计类型时,我得到数字。但当我提醒小计时,我得到 NaN。我缺少什么?谢谢。
最佳答案
When I
alert()
thetypeof subtotal
, I getnumber
.
typeof NaN
在 JavaScript 中是 number
。
But then when I alert the subtotal, I get
NaN
.
NaN
是有毒的,如果它是任何算术的一部分,结果就是 NaN
。
最可能的原因是 parseInt()
返回 NaN
。这些元素的 value
属性是空字符串吗?
关于Javascript、表单验证、获取 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7884279/