在jquery中,代码希望按列对除第一行以外的所有数据求和.我如何通过以下代码实现它

In jquery code want to sum all Data by column wise except first row. how can i acheive it by following code?

本文关键字:代码 数据 求和 实现 何通过 一行 希望 jquery      更新时间:2023-09-26

我使用以下代码来计算和。

$(".income_table tr:last td:not(:first,:nth-child(2),:nth-child(3))").text(function(i) {
    var t = 0;
    $(this).parent().prevAll().find("td:nth-child("+(i + 4)+")").each(function() {
        t += parseInt( $(this).text(), 10 ) || 0;
    });
    return t;
}); 

您可以使用slice()方法跳过第一行。神奇的是,prevAll()以相反的顺序为您提供所有的前置。

检查此项:

$(".income_table tr:last td:not(:first,:nth-child(2),:nth-child(3))").text(function(i) {
    var t = 0;
    $(this).parent().prevAll().slice(0, -1).find("td:nth-child("+4+")").each(function() {
        t += parseInt(  $(this).text(), 10 ) || 0;
    });
    return t;
}); 

我已经将函数中的第二行更改为搜索第四列。

试用:

JSFiddle