当前位置: 编程技术>WEB前端
用Jquery选择器计算table中的某一列某一行的合计
来源: 互联网 发布时间:2014-08-25
本文导语: 利用Jquery选择器,计算table中的某一列,某一行的合计,非常方便。下面以计算行合计为例: 核心算法: $('#tableId tr').each(function() { $(this).find('td:eq(columnIndex)').each(function() { totalAmount += parseFloat($(this).text()); }) }); 下面是...
利用Jquery选择器,计算table中的某一列,某一行的合计,非常方便。下面以计算行合计为例:
核心算法:
$('#tableId tr').each(function() {
$(this).find('td:eq(columnIndex)').each(function() {
totalAmount += parseFloat($(this).text());
})
});
下面是案例代码
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
效果图: