jquery计算表格列,求和 客官°小女子只卖身不卖艺 2023-10-02 10:06 2阅读 0赞 jquery计算表格列,求和 有时候,需要统计网页上数据总数,系统没有提供该功能,需要手动计算,于是设计了这段代码 效果展示: ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6I6r6Iul5aSV56a-_size_17_color_FFFFFF_t_70_g_se_x_16][] 核心代码: $(function() { var totalRow = 0; $('#overTimes tr').each(function() { $(this).find('td:eq(1)').each(function() { totalRow += parseFloat($(this).text()); }); }); console.log('计算共有:' + totalRow + '小时'); }); 代码示例:求表格第二例和 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> $(function() { var totalRow = 0; $('#overTimes tr').each(function() { $(this).find('td:eq(1)').each(function() { totalRow += parseFloat($(this).text()); }); }); console.log('计算共有:' + totalRow + '小时'); }); </script> </head> <body> <table id="overTimes" rules="all"> <tr> <th>Header0</th> <th>Header1</th> <th>Header2</th> <th>Header3</th> <th>Header4</th> </tr> <tr> <td>1.25</td> <td>1.13</td> <td>2</td> <td>2.6</td> <td>5.3</td> </tr> <tr> <td>8.6</td> <td>1.32</td> <td>2.4</td> <td>5.2</td> <td>5.6</td> </tr> <tr> <td>6.5</td> <td>3</td> <td>9.4</td> <td>4.2</td> <td>2.3</td> </tr> </table> </body> </html> [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA6I6r6Iul5aSV56a-_size_17_color_FFFFFF_t_70_g_se_x_16]: https://img-blog.csdnimg.cn/20210922131729564.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6I6r6Iul5aSV56a-,size_17,color_FFFFFF,t_70,g_se,x_16
还没有评论,来说两句吧...