最后一个单元格中显示的列单元格的Jquery总和

时间:2014-03-03 03:42:51

标签: jquery string numbers calculator valueconverter

我正在尝试创建一个onload脚本,它将从每个表格单元格中获取成本(跳过第一个$符号),将它们一起添加并使用$符号显示总成本。它似乎部分工作,因为“console.log(sum);”返回正确的和值,但我在整个单元格中得到一个奇怪的 NaN 结果。有什么想法吗?请看下面。 提前谢谢你,Attila

function colSum() {
var sum = 0;
var totalCost ="";
//iterate through each input cell and add to sum variable
$('td.cost').each(function() {     
    sum += parseFloat(($(this).text().substr(1)));
console.log(sum);  
$('#sumTotal').html(sum);              
});

}

HTML源代码:

<table id="BookTable" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="33%" scope="col"><h4>Title</h4></th>
<th width="18%" scope="col"><h4>Author</h4></th>
<th width="15%" scope="col"><h4>Publisher</h4></th>
<th width="6%" scope="col"><h4>Year</h4></th>
<th width="13%" scope="col"><h4>Cover Artist</h4></th>
<th width="9%" scope="col"><h4>Genre</h4></th>
<th width="6%" scope="col"><h4>Cost</h4></th>
</tr>
<tr>
<td width="33%">And Kill Once More </td>
<td width="18%">Al Fray </td>
<td width="15%">Graphic Mystery (1st)</td>
<td width="6%">1955</td>
<td width="13%">Saul Levine </td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$7.11</td>
</tr>
<tr>
<td width="33%">The Big Sleep</td>
<td width="18%">Raymond Chandler </td>
<td width="15%">New Avon Library (1st)</td>
<td width="6%">1943 </td>
<td width="13%">Paul Stahr  </td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$43.88</td>
</tr>
<tr>
<td width="33%">The Insidious Doctor Fu Manchu</td>
<td width="18%">Sax Rohmer </td>
<td width="15%">Pyramid (1st)</td>
<td width="6%">1961 </td>
<td width="13%">Mort Engle </td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$1.69</td>
</tr>
<tr>
<td width="33%">The Window At The White Cat</td>
<td width="18%">Mary Roberts Rinehart</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1938</td>
<td width="13%">Gerald Gregg</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$12</td>
</tr>
<tr>
<td width="33%">Steve Bentley's Calypso Caper                       </td>
<td width="18%">Robert Dietrich</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1961 </td>
<td width="13%">Tom Miller </td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$7</td>
</tr>
<tr>
<td width="33%">Dead Man's Diary and a Taste for Cognac</td>
<td width="18%">Brett Halliday</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1964</td>
<td width="13%">Robert McGinnis</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$2</td>
</tr>
<tr>
<td width="33%">House of Darkness</td>
<td width="18%">Allan MacKinnon</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1947</td>
<td width="13%">Gerald Gregg</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$3</td>
</tr>
<tr>
<td width="33%">The Panic Stricken</td>
<td width="18%">Mitchell Wilson</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1948 </td>
<td width="13%">Gerald Gregg?</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$6</td>
</tr>
<tr>
<td width="33%"> Hang By Your Neck</td>
<td width="18%">Henry kane</td>
<td width="15%">Dell (1st)</td>
<td width="6%">1949</td>
<td width="13%">Victor Kalin</td>
<td width="9%">Mystery</td>
<td width="6%" class="cost">$7</td>
</tr>
<tr>
<td>Odor of Violets</td>
<td>Baynard Kendrick </td>
<td>Dell (1st)</td>
<td>1947</td>
<td>Gerald Gregg</td>
<td>Mystery</td>
<td class="cost">$2</td>
</tr>
<tr>
<td>Top of the Heap</td>
<td>A.A.Fair (Erle Stanley Gardner)</td>
<td>Dell</td>
<td>1954</td>
<td>Griffith Foxley</td>
<td>Mystery</td>
<td class="cost">$5</td>
</tr>
<tr>
<td>Gods and Goddesses In Art and Legend</td>
<td>Herman J. Wechsler</td>
<td>Pocket Book (1st)</td>
<td>1950</td>
<td>N/A (photo)</td>
<td>Art</td>
<td class="cost">$0.50</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>Total:</td>
<td id="sumTotal"></td>
</tr>
</table>

1 个答案:

答案 0 :(得分:0)

修正了js:

var sum=0
//iterate through each input cell and add to sum variable
$('td.cost').each(function() {     
    sum += parseFloat(($(this).text().substr(1)));
    console.log(sum);  
    $('#sumTotal').html(sum);              
});

工作示例:http://jsfiddle.net/4PZFL/1/