我有多个带有类名.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);
});
}
答案 0 :(得分: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);
});
答案 1 :(得分:1)
function paymentData()
{
$('.paymentamount').keyup(function(){
var amountSum=0;
$('.paymentamount').each(function(){
amountSum+=parseFloat($.trim($(this).val()));
});
console.log(amountSum);
});
}
答案 2 :(得分: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);
});
答案 3 :(得分: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
答案 4 :(得分:-1)
你应该尝试:
amountSum+=parseInt($(this).val());