首页 > 解决方案 > 喜欢 jQuery 求和值

问题描述

我喜欢用脚本对所有值求和,但得到结果:total:NaN (需要对除第一列之外的所有列求和)

代码 PHP 和脚本在同一个文件中,这是我的代码:

对于 php:

<table id="table">
 <thead class="thead-dark">
 <tr class="titlerow">
 <th>Col1</th>
 <th>Col2</th>
 <th>Col3</th>
 <th>Col4</th>
 <th>col5</th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <?php
 include("conn.php");

$result = mysql_query("SELECT id,name,col1,col2 FROM table GROUP BY name");

 while($test = mysql_fetch_array($result))
 {
 $id = $test['id']; 
 echo"<td class='rowDataSd'>".$test['col1']."</td>";
 echo"<td class='rowDataSd'>".$test['col1']."</td>";
 echo"<td class='rowDataSd'>".$test['col2']."</td>";
 echo"<td class='rowDataSd'>".$test['col2']."</td>";
 echo"<td class='rowDataSd'>".$test['col2']."</td>";
 echo "</tr>";
 }
 mysql_close($conn);
 echo '<tfoot>
    <tr class="totalColumn">
        <td>.</td> 
        <td class="totalCol">Total:</td> 
        <td class="totalCol">Total:</td> 
        <td class="totalCol">Total:</td> 
        <td class="totalCol">Total:</td> 
        <td class="totalCol">Total:</td> 
    </tr>
</tfoot>';
 ?>
</table>

对于脚本:

   var totals=[0,0,0];
    $(document).ready(function(){
        var $dataRows=$("#table tr:not('.totalColumn, .titlerow')");
        $dataRows.each(function() {
            $(this).find('.rowDataSd').each(function(i){        
                totals[i]+=parseInt( $(this).html());
            });
        });
        $("#table td.totalCol").each(function(i){  
            $(this).html("total:"+totals[i]);
        });
    });

确切的问题在哪里?

标签: phpjquery

解决方案


我根本不会用 jQuery 来总结它们。我会在 PHP 中做到这一点:

...
<tr> <!--- this tr is out of place, needs to be in the loop -->
 <?php
  //include("conn.php"); this should be require as it's necessary
  //include will ignore missing files, require with throw an error
  //this code will not work without that file, so let PHP tell you
  //when it goes missing, instead of wondering why your DB calls don't work

  require "conn.php"; // inlude/require are not functions they will work with (...) but it's not best practice.

 $result = mysql_query("SELECT id,name,col1,col2 FROM table GROUP BY name");

//define default values for increment without errors
$totals = ['col1'=>0,'col2'=>0,'col3'=>0,'col4'=>0,'col5'=>0];
while($test = mysql_fetch_array($result))
{
   $id = $test['id']; 
   echo "<tr>";  //-- this is the tr from above --
   //use a loop if the HTML is all the same
   for($i=1;$i<=5;++$i){
     $key = 'col'.$i;
     echo"<td class='rowDataSd'>".$test[$key]."</td>";
     //simply add the values on each iteration (sum)
     $totals[$key] += $test[$key];
   }
   echo "</tr>"; //-- without fixing the open tag as mentioned above, your HTML would be messed up --
} 
 mysql_close($conn);
 echo '<tfoot>';
    echo '<tr class="totalColumn">';
      echo '<td>.</td>';
    for($i=1;$i<=5;++$i){ 
       echo '<td class="totalCol">total:'.$totals['col'.$i].'</td>';
    } 
    echo '</tr>';
 echo '</tfoot>';
 ...

如果这些东西没有动态变化(它似乎没有),就没有必要用 Javascript 来做。

您还可以使用简单的 for 循环来减少代码。

干杯!


推荐阅读