2
    var discount_amount = parseFloat($('#amtdelivary').val());
    var other_discount = parseFloat($('#othdis').val());
    calculate_total_amount=Math.round(sub_total1 + tax_amount +discount_amount-other_discount);
    $('#fnltot').val(calculate_total_amount);

当我将 other_discount 文本字段保留为空时,它显示一个 NaN。如果文本字段为 0,则其功能准确,但当该字段设置为空时,其结果为 NaN。

    if(isNaN(other_discount)){
                other_discount=0;
            }

我厌倦了这些,但它不起作用。

4

3 回答 3

3

|| 0在你parseFloat喜欢下面的后面附加。如果 value 返回为NaN或任何falsy值,则它将返回0而不是NaN.

var discount_amount = parseFloat($('#amtdelivary').val()) || 0;
var other_discount = parseFloat($('#othdis').val()) || 0;

var discount_amount = parseFloat();
console.log(discount_amount);

// Your if condition should also work.
if(isNaN(discount_amount)) {
  discount_amount = 0;
}
console.log(discount_amount);

// Alternate and single line solution.
discount_amount = parseFloat() || 0;
console.log(discount_amount);

于 2020-06-15T06:01:44.330 回答
2

在简单的 java 脚本中,您可以在给定函数中传递参数,如果为 true,则为 NUMBER,如果为 false,则为 NOT A NUMBER。

  function IsNaN(value) {
        if (Number.isNaN(parseInt(value)) === true)
            return true;
        return false;
    }
于 2020-06-15T06:14:13.190 回答
1

尝试这个

if (other_discount == '' || other_discount ==  null) {
    other_discount = 0
}
于 2020-06-15T06:04:16.760 回答