有一个测试脚本想要对值求和,但要得到结果:total:NaN(需要对除第一列之外的所有列求和。从2和6列开始) 该表仅包含2列和6列中的数字。
PHP和脚本代码位于同一文件中,这是我的代码
这是脚本:
<script>
var totals=[0,0,0,0,0];
$(document).ready(function(){
var $dataRows=$("#mytable tr:not('.totalColumn, .titlerow')");
$dataRows.each(function() {
$(this).find('.rowDataSd').each(function(i){
totals[i]+=parseInt( $(this).html());
});
});
$("#mytable td.totalCol").each(function(i){
$(this).html(totals[i]);
});
});
</script>
并且php具有以下代码:
<table id="mytable">
<thead>
<tr class="titlerow">
<th scope="col">Column1</th>
<th>Column2</th>
<th>Column3</th>
<th>Column4</th>
<th>Column5</th>
<th>Column6</th>
</tr>
</thead>
<tbody>
<tr>
<?php
include("conn.php");
$result = mysql_query("SELECT * FROM table1 GROUP BY name");
while($test = mysql_fetch_array($result))
{
$id = $test['id'];
echo"<td>".$test['name']."</td>";
echo"<td class='rowDataSd'>".$test['value1']."</td>";
echo"<td class='rowDataSd'>".$test['value2']."</td>";
echo"<td class='rowDataSd'>".$test['value3']."</td>";
echo"<td class='rowDataSd'>".$test['value4']."</td>";
echo"<td class='rowDataSd'>".$test['value5']."</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>
答案 0 :(得分:0)
当parseInt
的值不正确时返回NaN,您可以使用以下命令清除数据:
$(this).text().replace(/[^0-9]/g, '');