Javascript 列的总和
请参考前面的问题: 我使用了艾曼的解决方案,但我编辑它以满足我的需要。它停止工作,我的代码如下所示:Javascript 列的总和,javascript,jquery,ajax,Javascript,Jquery,Ajax,请参考前面的问题: 我使用了艾曼的解决方案,但我编辑它以满足我的需要。它停止工作,我的代码如下所示: 如何解决jquery计算错误的问题 如何按行计算总计 我需要完全相同的类名 我不太确定您想要什么,但如果您只想按列对所有行求和,请参见下文 var totalsByRow = [0, 0, 0, 0, 0]; var totalsByCol = [0, 0, 0, 0, 0]; $(document).ready(function() { var $dataRows = $("#sum
我不太确定您想要什么,但如果您只想按列对所有行求和,请参见下文
var totalsByRow = [0, 0, 0, 0, 0];
var totalsByCol = [0, 0, 0, 0, 0];
$(document).ready(function() {
var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");
$dataRows.each(function(i) {
$(this).find('td:not(.totalRow)').each(function(j) {
totalsByCol[j] += parseInt($(this).html());
totalsByRow[i] += parseInt($(this).html());
});
});
for (var i = 0; i < totalsByCol.length - 1; i++) {
totalsByCol[totalsByCol.length - 1] += totalsByCol[i];
}
$("#sum_table td.totalCol").each(function(i) {
$(this).html("total:" + totalsByCol[i]);
});
$("#sum_table td.totalRow").each(function(i) {
$(this).html("total:" + totalsByRow[i]);
});
});
var totalsByRow=[0,0,0,0,0];
var totalsByCol=[0,0,0,0,0];
$(文档).ready(函数(){
var$dataRows=$(“#sum_table tr:not('.totalColumn.titlerow'));
$dataRows.每个(函数(i){
$(this).find('td:not(.totalRow')).each(函数(j){
totalsByCol[j]+=parseInt($(this.html());
totalsByRow[i]+=parseInt($(this.html());
});
});
对于(变量i=0;i我不太确定您想要什么,但如果您只想按列对所有行求和,请参见下文
var totalsByRow = [0, 0, 0, 0, 0];
var totalsByCol = [0, 0, 0, 0, 0];
$(document).ready(function() {
var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");
$dataRows.each(function(i) {
$(this).find('td:not(.totalRow)').each(function(j) {
totalsByCol[j] += parseInt($(this).html());
totalsByRow[i] += parseInt($(this).html());
});
});
for (var i = 0; i < totalsByCol.length - 1; i++) {
totalsByCol[totalsByCol.length - 1] += totalsByCol[i];
}
$("#sum_table td.totalCol").each(function(i) {
$(this).html("total:" + totalsByCol[i]);
});
$("#sum_table td.totalRow").each(function(i) {
$(this).html("total:" + totalsByRow[i]);
});
});
var totalsByRow=[0,0,0,0,0];
var totalsByCol=[0,0,0,0,0];
$(文档).ready(函数(){
var$dataRows=$(“#sum_table tr:not('.totalColumn.titlerow'));
$dataRows.每个(函数(i){
$(this).find('td:not(.totalRow')).each(函数(j){
totalsByCol[j]+=parseInt($(this.html());
totalsByRow[i]+=parseInt($(this.html());
});
});
对于(变量i=0;i本质上,你想把所有的“代码”TD < /代码>元素放在中间行中。每次在新的
td
上循环时,您都希望将其值添加到该行的最后一个td(除非它是该行的最后一个td
),以及共享其索引的最后一行的td
$("#sum_table tr:not(:first,:last)").each(function(c,row) {
$("td",row).text(function(i,t) {
var n = parseInt( t, 10 ) || 0;
$(this).nextAll(":last-child").text(function(a,o) {
return n + ( parseInt( o, 10 ) || 0 );
});
$(row).nextAll("tr:last").find("td:nth-child("+(++i)+")").text(function(a,o){
return "Total: " + ( n + ( parseInt( o.replace(/[^\d]/g,""), 10 ) || 0 ) );
});
});
});
这应该适用于任何大小的表,而不限于x列或y行
小提琴:
评论
我鼓励您阅读下面示例中的注释,因为它们将帮助您了解每一行的情况
// For each table row that is not first or last
$("#sum_table tr:not(:first,:last)").each(function(c,row) {
// For each td within this row
$("td",row).text(function(i,t) {
// Determine numerical value of this td's content
var n = parseInt( t, 10 ) || 0;
// Find last td in this row, change its text
$(this).nextAll(":last-child").text(function(a,o) {
// Increment its value with the value of current TD
return n + ( parseInt( o, 10 ) || 0 );
});
// Find last row, and td within of same index as current td, change its text
$(row).nextAll("tr:last").find("td:nth-child("+(++i)+")").text(function(a,o){
// Increment its value (removing non-numbers) with the value of current td
return "Total: " + ( n + ( parseInt( o.replace(/[^\d]/g,""), 10 ) || 0 ) );
});
// End our td loop
});
// End our tr loop
});
<>本质上,你要把所有的“代码> TD < /Cord>元素放在中间行中。每次在新的
td
上循环时,您都希望将其值添加到该行的最后一个td(除非它是该行的最后一个td
),以及共享其索引的最后一行的td
$("#sum_table tr:not(:first,:last)").each(function(c,row) {
$("td",row).text(function(i,t) {
var n = parseInt( t, 10 ) || 0;
$(this).nextAll(":last-child").text(function(a,o) {
return n + ( parseInt( o, 10 ) || 0 );
});
$(row).nextAll("tr:last").find("td:nth-child("+(++i)+")").text(function(a,o){
return "Total: " + ( n + ( parseInt( o.replace(/[^\d]/g,""), 10 ) || 0 ) );
});
});
});
这应该适用于任何大小的表,而不限于x列或y行
小提琴:
评论
我鼓励您阅读下面示例中的注释,因为它们将帮助您了解每一行的情况
// For each table row that is not first or last
$("#sum_table tr:not(:first,:last)").each(function(c,row) {
// For each td within this row
$("td",row).text(function(i,t) {
// Determine numerical value of this td's content
var n = parseInt( t, 10 ) || 0;
// Find last td in this row, change its text
$(this).nextAll(":last-child").text(function(a,o) {
// Increment its value with the value of current TD
return n + ( parseInt( o, 10 ) || 0 );
});
// Find last row, and td within of same index as current td, change its text
$(row).nextAll("tr:last").find("td:nth-child("+(++i)+")").text(function(a,o){
// Increment its value (removing non-numbers) with the value of current td
return "Total: " + ( n + ( parseInt( o.replace(/[^\d]/g,""), 10 ) || 0 ) );
});
// End our td loop
});
// End our tr loop
});
SO问题应该是完全独立的,而不是需要链接到上一个问题的后续问题。如果您正在寻找一个可以处理任意行数和列数的解决方案,则可能重复@Redbox,我在下面提供了一个问题。SO问题应该是完全独立的,而不是需要链接到上一个问题的后续问题。可能重复@Redbox如果您正在寻找一个可以处理任意行数和列数的解决方案,我在下面提供了一个。按列求和现在可以了,按行求和如何?需要对同一行值求和并将其放入类中totalRow@Redbox请检查更新后的帖子,查看按行和列计算的总计。@按列求和的红框已更新,按行计算如何?需要对同一行值求和并将其放入类中totalRow@Redbox请检查更新后的帖子,查看按行和列列出的总计。@Redbox已更新,查看总计。