我有一些代码可以在选中复选框后计算价格和数量,但是如果从默认值 1 更改为数量字段而不取消选中并重新选中复选框并重新触发复选框更改,我将无法实现对数量字段的更改.
<table>
<tr>
<td width="10%"> </td>
<td width="20%">Item</td>
<td width="10%">Price</td>
<td width="10%">Quantity</td>
<td width="10%">Total</td>
</tr>
<tr>
<td >
<input type="checkbox" name="Extras" value="id" />
</td>
<td>Test Item 1</td>
<td class="price">12.50</td>
<td><input name="qty" class="quantity" value="1"></td>
<td class="total"></td>
</tr>
<tr>
<td >
<input type="checkbox" name="Extras" value="id" />
</td>
<td>Test item 2</td>
<td class="price">20</td>
<td><input name="qty" class="quantity" value="1"></td>
<td class="total"></td>
</tr>
</table>
jQuery
$(document).ready(function() {
$('input:checkbox').change(function() {
var $this = $(this),
parentRow = $this.closest('tr'),
totalCell = parentRow.find('.total');
if ($this.prop('checked') === true) {
var price = parseFloat(parentRow.find('.price').html());
var quantity = parseFloat(parentRow.find('.quantity').val(
// assuming I'd need a $('input:text').change(function() {
// etc at this point but all i've tried doesn't work
));
totalCell.text(price * quantity)
}
else totalCell.text('')
});
});
希望这是有道理的