$(document).ready(function() {
$(':checkbox').change(function() {
$('input:checkbox:checked').filter(function() {
var sum = 0;
var Something = parseFloat($(this).closest('tr').find('td:eq(2)').text()) + sum;
var value = Something;
alert(Something);
$('#total').html(value);
});
});
});
<table class="table table-bordered" id="echotable">
<tr>
<th class="field-label col-xs-1 primary">ID</th>
<th class="field-label col-xs-5 primary">Name Of Service</th>
<th class="field-label col-xs-3 primary">Price</th>
<th class="field-label col-xs-3 primary">Total</th>
</tr>
<?php foreach($echo_details as $post){?>
<tr class="active" id="myrow">
<td class="field-label col-xs-1 primary"><?php echo $post->echo_id;?></td>
<td class="field-label col-xs-5 primary"><?php echo $post->echo_scan;?></td>
<td class="field-label col-xs-3 primary" id="price"><?php echo $post->price;?></td>
<td class="field-label col-xs-3 primary"><input type="checkbox" id="myCheckbox" /></td>
</tr>
<?php }?>
<tr>
<td colspan="3" class="total"><strong>TOTAL</strong></td>
<td id="total"></td>
</tr>
我動態從數據庫中獲取的值。當用戶選擇多個複選框時,我將獲得所有價格值。現在我需要添加所有的價格值,並且需要顯示總成本。我怎樣才能做到這一點?動態地添加所述多個所選擇的值,並且計算總
在'foreach'循環中的HTML是創建具有相同'id'屬性,這是使你的HTML無效的多個元素。你應該改變'價格'和'myCheckbox'來代替使用類 –