jquery对重复行数组中的所有值求和

时间:2014-11-04 02:10:00

标签: jquery

大家好我无法找到解决方案;

<tr><td> Some Code here <input name="forwardings[nodes][amount][]" class="repeatingClass"> some more code here </td> </tr>
<tr><td> Some Code here <input name="forwardings[nodes][amount][]" class="repeatingClass"> some more code here </td> </tr>
<tr><td> Some Code here <input name="forwardings[nodes][amount][]" class="repeatingClass"> some more code here </td> </tr>

我需要总结所有的repeatClass值,当我从其中一个中集中注意力时,我尝试了类似的东西,但是无法处理这个;

 $('.repeatingClass').focusout(function(){
     var totalPoints = 0;
   $('.repeatingClass').each(function(){
     totalPoints += $('.repeatingClass').val();
   });
   alert(totalPoints);
 });

我该怎么办? 问候

1 个答案:

答案 0 :(得分:0)

输入中的空值被解释为字符串。因此,您应首先查看是否有值,如果有,请在致电parseIntparseFloat

后将其添加到总计中
$(function(){

     $('.repeatingClass').focusout(function(){
         var totalPoints = 0;

       $('.repeatingClass').each(function(){

           if ($(this).val())
               totalPoints += parseInt($(this).val());
       });

       alert(totalPoints);

     });

});

Here是一个小提琴