parseFloat返回NaN javascript编号

时间:2015-11-25 20:37:30

标签: javascript jquery html

我遇到此问题,此代码无法计算<tr>代码中的任何内容。

function CashCategory()
        {
            $("#cashAmount"+counterCash).keydown(function () {
                setTimeout(function () {
                  var z1=$("#cashAmount"+counterCash).val();

                  var sum3 = 0;
                    $('.priceCash').each(function() {
                        sum3 += parseFloat($(this).text());
                    });
                    //$('#grandTotalGold').html(sum2.toFixed(2));
                    $('#grandTotalCash').val(sum3.toFixed(2));
                    //$('#grandTotalCash').text(sum3.toFixed(2));
                }, 0);

            });
        }

HTML代码

<td class="priceCash">
  <input type="text" id="cashAmount1" class="form-control" value="">
</td>

z1产生值但是在计算时。它当NaN

3 个答案:

答案 0 :(得分:2)

我没有看到你在任何地方使用z1进行计算。此外,$(this).text()应始终为空,因为td.priceCash内部没有任何文字。

也许你真的打算这样做?

    $("#cashAmount"+counterCash).keydown(function () {
            setTimeout(function () {
              var sum3 = 0;
                $('.priceCash').each(function(i,el) {
                    sum3 += parseFloat($("input",this).val());
                });

答案 1 :(得分:1)

 $('.priceCash').each(function() {
      sum3 += parseFloat($(this).text());
    });

传递给parseFloat的文字将是一个空字符串,所以很自然地会得到NaN

答案 2 :(得分:0)

看起来您需要将counterCash作为变量传递给函数:

功能CashCategory(counterCash){...}