利用Jquery选择器,计算table中的某一列,某一行的合计,非常方便。下面以计算行合计为例:
核心算法:
$('#tableId tr').each(function() {
$(this).find('td:eq(columnIndex)').each(function() {
totalAmount += parseFloat($(this).text());
})
});
Demo:
Jquery计算table行合计$(document).ready(function() {
var totalRow = 0
$('#mytable tr').each(function() {
$(this).find('td:eq(2)').each(function(){
totalRow += parseFloat($(this).text());
});
});
$('#totalRow').append('
合计'+totalRow+'');});
11 | 12 | 13 | 14 |
21 | 22 | 23 | 24 |
效果图: