开发者

iterate through each selector jquery

开发者 https://www.devze.com 2022-12-23 19:44 出处:网络
I\'m having problems calculating stuff on my web app. Here is the scenario: I have a html markup like this:

I'm having problems calculating stuff on my web app. Here is the scenario:

I have a html markup like this:

<table>
   <tr>
       <td><span class="sub_total">10</span></td>
   </tr>
   <tr>
       <td><span class="sub_total">10</span></td>
   </tr>
   <tr>
       <td><span class="sub_total">10</span></td>
   </tr>
</table>

<p><span id="total"></span></p>

I would like to calculate the main total of all the sub totals:

    var total;
    $('.sub_total').each(function(){
        total = total + parseInt($(this).text());
    });

    $('#total').text(total);

But I can't get this to work. I get开发者_如何学运维 a NaN notification..


You have to initialize total to 0:

var total = 0; // <-- initialize to zero

$('.sub_total').each(function(){
    total = total + parseInt($(this).text());
});

$('#total').text(total);
0

精彩评论

暂无评论...
验证码 换一张
取 消