我有一个像这样的html表
<table>
<thead>
<tr>
<th> column1 </th>
<th> column2 </th>
<th> total column </th>
</tr>
</thead>
<tbody>
<tr>
<td class="td-caption"> title of the grouped rows 1 </td>
</tr>
<tr>
<td> value 1</td>
<td class="total_class"> row total</td>
</tr>
<tr>
<td> Total </td>
<td class="total_tr"> the sum of the rows total <td>
</tr>
<tr>
<td class="td-caption"> title of the grouped rows 2 </td>
</tr>
. . .
<tbody>
</table>
所以我需要加上“total_class”的值,并将其放入 total_tr
,但如果下一个tr具有类 td-caption
这个 total_tr
应该到此结束,然后重新开始下一节
我已经有了这个小代码来获取总数,但是我不是从0开始计算每个部分的总数,所以总数一直累加到最后
$(document).ready(function(){
// Loop through each div element with the class total_class
let numbers = []
$(".total_class").each(function(){
// Check the td is not empty
if($(this).is(":empty")){
$(this).css("background", "yellow");
} else {
// get the numbers of every td, parse to int and add to numbers array
let number = $(this).text();
let intnum = parseInt(number);
numbers.push(intnum);
}
});
// Loop over number's array to get the total
var total = 0;
for (var i = 0; i <numbers.length; i++) {
total +=numbers[i] << 0;
}
// Display on total_tr
$('.total_tr').text(total);
});
暂无答案!
目前还没有任何答案,快来回答吧!