jQuery 求和行添加所有单元格 [英] jQuery sum rows adding all cells

查看:25
本文介绍了jQuery 求和行添加所有单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了有关添加表列的各种 SO 问题,但在行上搜索的问题并不多.这是我到目前为止所拥有的:

I've searched various SO questions about adding table columns but not many on rows. Here's what I have so far:

HTML

<table>
    <thead>
        <tr>
            <th>MAX ATK</th>
            <th>MAX DEF</th>
            <th>MAX HP</th>
            <th>Overall</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="combat">8170</td>
            <td class="combat">6504</td>
            <td class="combat">6050</td>
            <td class="total-combat"></td>
        </tr>
        <tr>
            <td class="combat">8500</td>
            <td class="combat">10200</td>
            <td class="combat">7650</td>
            <td class="total-combat"></td>
        </tr>
        <tr>
            <td class="combat">9185</td>
            <td class="combat">7515</td>
            <td class="combat">9185</td>
            <td class="total-combat"></td>
        </tr>
    </tbody>
</table>

jQuery

 $(document).ready(function () {
     var sum = 0;
     $('tr').find('.combat').each(function () {
         var combat = $(this).text();
         if (!isNaN(combat) && combat.length !== 0) {
             sum += parseFloat(combat);
         }
     });

     $('.total-combat').html(sum);
 });

你可以在这里看到我的小提琴.

You can see my fiddle here.

问题:它会查找所有出现的 .combat 并将它们相加,而不是仅查找当前行中出现的 .combat.所以,而不是:

Problem: It's finding ALL occurrences of .combat and adding them up instead of only finding occurrences of .combat within the current row. So instead of:

MAX ATK MAX DEF MAX HP  Overall
 8170    6504    6050   20724
 8500    10200   7650   26350
 9185    7515    9185   25885

我得到:

MAX ATK MAX DEF MAX HP  Overall
 8170    6504    6050   72959
 8500    10200   7650   72959
 9185    7515    9185   72959

我尝试使用 closest() 认为这会告诉它找到父 tr 并且只在该 tr 中添加 tds,如下所示:

I tried using closest() thinking that would tell it to find the parent tr and only add tds within that tr, like so:

     $('.combat').closest('tr').each(function () {

但这没有用:(

任何帮助将不胜感激!

推荐答案

你需要使用 each 循环

You need to use a each loop

$(document).ready(function () {
    //iterate through each row in the table
    $('tr').each(function () {
        //the value of sum needs to be reset for each row, so it has to be set inside the row loop
        var sum = 0
        //find the combat elements in the current row and sum it 
        $(this).find('.combat').each(function () {
            var combat = $(this).text();
            if (!isNaN(combat) && combat.length !== 0) {
                sum += parseFloat(combat);
            }
        });
        //set the value of currents rows sum to the total-combat element in the current row
        $('.total-combat', this).html(sum);
    });
});

演示:小提琴

更短的求和方法:使用一元加号 - Demo

A shorter way to sum: Using unary plus - Demo

这篇关于jQuery 求和行添加所有单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆