我使用php/mysql中的循环生成多个表,结构相同但值不同。我有一个脚本来总结所有值并显示结果,但只有当值在同一行时才有效。我只需要总结“precio”列。通过jum总结colum值可以在多个表中获得总数
这是一个HTML。这是表的结构:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('tr').each(function() {
var sum = 0
$(this).find('.precio').each(function() {
var precio = $(this).text();
if (!isNaN(precio) && precio.length !== 0) {
sum += parseFloat(precio);
}
});
$('.total_prt', this).html(sum);
});
});
</script>
<table class="table table-striped">
<thead>
<tr>
<th width="70%">Descripción</th>
<th width="20%">Precio</th>
</tr>
</thead>
<tbody>
<tr>
<td>Display</td>
<td class="precio">1300</td>
</tr>
<tr>
<td>Keyboard</td>
<td class="precio">300</td>
</tr>
<tr>
<td><b>Total:</b></td>
<td class="total_prt"><b>$</b></td>
</tr>
</tbody>
</table>
<br>
<table class="table table-striped">
<thead>
<tr>
<th width="70%">Descripción</th>
<th width="20%">Precio</th>
</tr>
</thead>
<tbody>
<tr>
<td>HDD</td>
<td class="precio">400</td>
</tr>
<tr>
<td>System</td>
<td class="precio">425</td>
</tr>
<tr>
<td>Something</td>
<td class="precio">350</td>
</tr>
<tr>
<td><b>Total:</b></td>
<td class="total_prt"><b>$</b></td>
</tr>
</tbody>
</table>
最佳,更容易。谢谢......我把js放在最后......我想我早点想到了,但是我犯了这个错误...... –