$(document).ready(function() { var totalNum = 0; $('#dataList tbody tr').each(function() { var text = $(this).find('td:nth-child(7)').text(); //这个7是从1数,不是从0数。 if(isNotEmpty(text)){ totalNum += parseInt($(this).find('td:nth-child(7)').text()); } }); var totalRow = '<tr><td style="text-align: right;" colspan="6">合计</td><td style="text-align: center;">' + totalNum + '</td> </tr>'; $('#dataList').append(totalRow); }); colspan="6" 这个td占6行。
function cal_total(){ var dateColumns = []; $('#dataList thead th').each(function(index) { console.log(index); if (index > 5) { // Skip the first 6 columns dateColumns.push(index); } }); console.log(dateColumns); var totals = Array(dateColumns.length).fill(0); $('#dataList tbody tr').each(function() { $(this).find('td').each(function(index) { if (dateColumns.includes(index)) { var value = parseInt($(this).text()) || 0; totals[index - 6] += value; } }); }); var totalRow = '<tr><td colspan="6">合计</td>'; totals.forEach(function(total) { totalRow += '<td>' + total + '</td>'; }); totalRow += '</tr>'; $('#dataList tbody').append(totalRow); }
站长微信:xiaomao0055
站长QQ:14496453