检查NaN条件不起作用

时间:2016-11-10 10:25:12

标签: javascript nan

在我的Page_load我正在使用一个JS函数。默认情况下,它会显示NaN所以我添加了检查NaN的条件,如下所示

var totalPaid = document.getElementById('txttotalPaid').value;
        var totLandVal = 0;

        totLandVal = parseInt(Expense) + parseInt(totalPaid)

        if (totLandVal == 0 || totLandVal == NaN) {

            document.getElementById('txtTotalLandVal').value = 0;
        }
        else {
            document.getElementById('txtTotalLandVal').value = totLandVal;
        }

但是文本框中的值仍然是Nan

4 个答案:

答案 0 :(得分:9)

请改用isNaN(),因为惊喜:

NaN == NaN  // false

答案 1 :(得分:3)

只需将totLandVal == NaN更改为isNaN(totLandVal)

答案 2 :(得分:1)

变化:

if (totLandVal == 0 || totLandVal == NaN)

if ( totLandVal == 0 || isNaN(totLandVal) )

答案 3 :(得分:1)

像这样使用:

var totalPaid = document.getElementById('txttotalPaid').value;
        var totLandVal = 0;

        totLandVal = parseInt(Expense) + parseInt(totalPaid)

        if (totLandVal == 0 || isNaN(totLandVal)) {

            document.getElementById('txtTotalLandVal').value = 0;
        }
        else {
            document.getElementById('txtTotalLandVal').value = totLandVal;
        }