zoukankan      html  css  js  c++  java
  • Jquery、js计算table列合计

    利用Jquery选择器,计算table中的某一列,某一行的合计,非常方便。下面以计算行合计为例:

    核心算法:

    $('#tableId tr').each(function() {

      $(this).find('td:eq(columnIndex)').each(function() {

        totalAmount += parseFloat($(this).text());

      })

    Demo

    <!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" >11</td>
            <td width="68" >12</td>
            <td width="62" >13</td>
            <td width="75" >14</td>
        </tr>
        <tr>
            <td width="63" >21</td>
            <td width="68" >22</td>
            <td width="62" >23</td>
            <td width="75" >24</td>
        </tr>
        <tr id="totalRow"></tr>
    </table>
    </body>
    </html>
  • 相关阅读:
    操作系统--进程间同步
    操作系统--进程间通信
    LeetCode-- Unique Binary Search Trees II
    STL源码--序列容器(一)
    操作系统--用户级线程和内核级线程
    非洲孩子
    寻找最大数(三)
    找点
    心急的C小加
    1044 拦截导弹——http://codevs.cn/problem/1044/
  • 原文地址:https://www.cnblogs.com/feiwenstyle/p/9519201.html
Copyright © 2011-2022 走看看