利用Jquery选择器,计算table中的某一列,某一行的合计,非常方便。下面以计算行合计为例:
核心算法:
view sourceprint?1 $('#tableId tr').each(function() {
2 $(this).find('td:eq(columnIndex)').each(function() {
3 totalAmount += parseFloat($(this).text());
4 })
5 });
下面是案例代码
view sourceprint?01
02
03
04
05
06
07
08 $(document).ready(function() {
09
10 var totalRow = 0
11 $('#mytable tr').each(function() {
12 $(this).find('td:eq(2)').each(function(){
13 totalRow += parseFloat($(this).text());
14 });
15 });
16
17 $('#totalRow').append('
18 });
19
20
21
22
23
11 | 12 | 13 | 14 |
21 | 22 | 23 | 24 |
40
41
效果图:
更多信息请查看IT技术专栏