1

我有多个带有类名的文本框.paymentamount。我想添加同一类的文本框的所有值,但我收到错误 nan.Code 是这个

function paymentData()
{
    $('.paymentamount').keyup(function(){
        var amountSum=0;
        $('.paymentamount').each(function(){
            amountSum+=parseInt(this.value);
        //console.log(this.value);
        });
        console.log(amountSum);

        });
}
4

5 回答 5

2
   1. $.trim($(this).val())
   2. make use of $(this).val()
   3. check before adding var match = /^[0-9]$/.test(value); 

对您的代码进行此修改后的最终代码

 $('.paymentamount').each(function(){
           var value = $.trim($(this).val()); 
           var match = /^[0-9]$/.test(value); 
           if(match)
             amountSum+=parseInt(value);
         //console.log(this.value);
         });
于 2012-05-15T08:04:48.580 回答
1
function paymentData()
{
    $('.paymentamount').keyup(function(){
        var amountSum=0;
        $('.paymentamount').each(function(){
            amountSum+=parseFloat($.trim($(this).val()));

        });
        console.log(amountSum);

        });
}
于 2012-05-15T08:09:52.327 回答
1

Hiya演示 http://jsfiddle.net/MaK2k/1/ 使用$.isNumeric http://jsfiddle.net/AKnvr/

**Code**


$('.paymentamount').keyup(function(){
   var amountSum=0;
    $('.paymentamount').each(function(){
        if (this.value != "")
           amountSum+=parseInt(this.value);

    });

    $(".paymentamountlable").text(amountSum);


    });

​</p>

于 2012-05-15T08:09:57.800 回答
0
$('.paymentamount').keyup(function(){
    var amountSum=0;
    $('.paymentamount').each(function(){
        if(parseInt(this.value) > 0)
        {
        amountSum = parseInt(this.value) + parseInt(amountSum);
        }
    }); 

 console.log(amountSum);
});



Try this
于 2012-05-15T08:20:25.600 回答
-1

你应该试试:

amountSum+=parseInt($(this).val());
于 2012-05-15T08:08:45.773 回答