zoukankan      html  css  js  c++  java
  • 用Jquery选择器计算table中的某一列某一行的合计

    核心算法:

       $('#tableId tr').each(function() { 
       $(this).find('td:eq(columnIndex)').each(function() { 
       totalAmount += parseFloat($(this).text());
       })
       });
    
    

    完整代码:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Jquery计算table行合计</title> 
    <script id="jquery_183" type="text/javascript" class="library" src="http://runjs.cn/js/sandbox/jquery/jquery-1.8.3.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     
    var totalRow = 0 
    $('#mytable tr').each(function() { 
    $(this).find('td:eq(2)').each(function(){ 
    totalRow += parseFloat($(this).text()); 
    }); 
    }); 
     
    $('#totalRow').append('<td>合计</td><td></td><td>'+totalRow+'</td><td></td>'); 
    }); 
    </script> 
     
    </head> 
    <body style="100%; height:100%;"> 
    <table id="mytable" border="1" width="37%"> 
    <thead> 
    </thead> 
    <tr> 
    <td width="63" >10</td> 
    <td width="68" >20</td> 
    <td width="62" >30</td> 
    <td width="75" >40</td> 
    </tr> 
    <tr> 
    <td width="63" >25</td> 
    <td width="68" >35</td> 
    <td width="62" >45</td> 
    <td width="75" >55</td> 
    </tr> 
    <tr id="totalRow"></tr> 
    </table> 
    </body> 
    </html>
    
    

    效果图:

  • 相关阅读:
    迟到的恶劣影响
    spring boot 向数据库写入海量数据
    分析 SQL 执行过程
    Mysql 索引 BTree 与 Hash
    Mysql 数据库设计
    Jdk 源码下载方式
    深入理解JVM虚拟机-周志明【第三版】
    Elasticsearch 查询实践
    MFC程序运行流程

  • 原文地址:https://www.cnblogs.com/newcapecjmc/p/11803326.html
Copyright © 2011-2022 走看看