简体   繁体   English

带有多个HTML表的jQuery Math

[英]Jquery Math with Multiple HTML tables

So I have 12 tables, one for each month of the year. 所以我有12张桌子,一年中的每个月一张。 Each table row has a cell that displays a value. 每个表格行都有一个显示值的单元格。 I would like to calculate the sum of these rows/cells for each table. 我想为每个表计算这些行/单元格的总和。 Right now I'm using a script that looks like this from a preview answer, but to duplicate it 12 times is not working and it's a lot of code. 现在,我正在使用一个脚本,该脚本从预览答案中看起来像这样,但是将其复制12次无法正常工作,并且其中包含很多代码。 Is there a shorter way I can do this? 有没有更短的方法可以做到这一点?

 <script type='text/javascript'> //<![CDATA[ $(window).load(function(){ var valueCells = document.querySelectorAll('.rowDataSdAugust'); var sum = 0; Array.prototype.forEach.call( valueCells, function(valueCell) { sum += parseFloat(valueCell.innerText); } ); var roughsum = sum /valueCells.length; var finalsum = roughsum.toFixed(2); document.querySelectorAll('.totalColAugust')[0].innerHTML = "<h1 style='color:green;'>"+ finalsum + "% GAIN</h1>"; }); //]]> </script> <script type='text/javascript'> //<![CDATA[ $(window).load(function(){ var valueCells = document.querySelectorAll('.rowDataSdJuly'); var sum = 0; Array.prototype.forEach.call( valueCells, function(valueCell) { sum += parseFloat(valueCell.innerText); } ); var roughsum = sum /valueCells.length; var finalsum = roughsum.toFixed(2); document.querySelectorAll('.totalColJuly')[0].innerHTML = "<h1 style='color:green;'>"+ finalsum + "% GAIN</h1>"; }); //]]> </script> 

Create two arrays - one for table names and other for total column names. 创建两个数组-一个用于表名,另一个用于总列名。 Then execute the same function number of times thru loop. 然后通过循环执行相同的功能次数。

    <script type='text/javascript'>
//<![CDATA[

    $(window).load(function(){

        var tableArray = new Array("rowDataSdJanuary","rowDataSdFebruary","rowDataSdMarch","rowDataSdApril","rowDataSdMay","rowDataSdJune","rowDataSdJuly","rowDataSdAugust","rowDataSdSeptember","rowDataSdOctober","rowDataSdNovember","rowDataSdDecember");
        var totalColArray = new Array("totalColJanuary","totalColFebruary","totalColMarch","totalColApril","totalColMay","totalColJune","totalColJuly","totalColAugust","totalColSeptember","totalColOctober","totalColNovember","totalColDecember");

        for(i=0; i<tableArray.length; i++) {     

            var valueCells = document.querySelectorAll('.'+tableArray[i]);
            var sum = 0;

            Array.prototype.forEach.call(
                valueCells,
                function(valueCell) {
                  sum += parseFloat(valueCell.innerText);
                }
            );
            var roughsum = sum /valueCells.length;
            var finalsum = roughsum.toFixed(2);
            document.querySelectorAll('.'+totalColArray[i])[0].innerHTML = "<h1 style='color:green;'>"+ finalsum + "% GAIN</h1>";            

        }        

    });

//]]> 
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM