Javascript、表单验证、获取 NaN

标签 javascript

在一个表单中,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() the typeof subtotal, I get number.

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/

相关文章:

javascript - Node js Express静态未收到客户端的post响应

javascript - 如何在 JavaScript 中获取键/值对中的值 - Google Trends API

javascript - 在 asp.net 中创建弹出窗口?

javascript - jQuery 倒计时在暂停时运行

javascript - 如何使用从 API 调用返回的 JSON 数据使 jquery 自动建议

javascript - 如何从 Canvas 中添加和删除(多个)图像。?

javascript - 我想在使用 jQuery 滚动到 View 时触发一次事件

php - 帮助使用 javascript 显示 php 时间/日期函数

javascript - Material UI(日期时间选择器): Invalid Date Format message after submission

javascript - Javascript 的某些部分无法运行,但仅在 iOS Safari 上运行