0
我試圖創建一個onload腳本,它將從每個表格單元中取出成本(跳過第一個$符號),將它們加在一起並用$符號表示總成本。它似乎工作的部分原因是「console.log(sum);」返回正確的總和值,但我得到一個奇怪的NaN結果在總細胞。任何想法爲什麼?請看下面。 謝謝你在前進,阿提拉Jquery列單元格的總和在最後一個單元格
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> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>Total:</td>
<td id="sumTotal"></td>
</tr>
</table>
可以嗎共享html示例 –
我已經添加了上面的HTML示例。 – reinhat