2014-03-03 106 views
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>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>&nbsp;</td> 
<td>Total:</td> 
<td id="sumTotal"></td> 
</tr> 
</table> 
+0

可以吗共享html示例 –

+0

我已经添加了上面的HTML示例。 – reinhat

回答

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/

相关问题