在表格中,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。我错过了什么?感谢。
答案 0 :(得分:2)
JavaScript中的{p>当我
alert()
typeof subtotal
时,我得到number
。
typeof NaN
为number
。
但是当我提醒小计时,我得到
NaN
。
NaN
有毒,如果它是任何算术的一部分,则结果为NaN
。
最可能的原因是parseInt()
正在返回NaN
。这些元素'value
属性是否为空字符串?
答案 1 :(得分:1)
parseInt(null)或parseInt('')是NaN,您应该确定document.form1.product1.value,document.form1.product2.value或document.form1.product3.value不为null或为空,并且是一个数字。