我想要每行的 2 列相乘。这是我的代码,也是一个脚本。此脚本有效,但仅针对第一行,此脚本的输出为:
第二行的结果是错误的:
<form>
<table id="my-table">
<?php
$query3 = mysqli_query($con,"select * from orders") or die("sdfsfs");
while($row3=mysqli_fetch_array($query3))
{
$quantity = $row3['quantity'];
$unit_price = $row3['unit_price'];
//$total = $quantity * $unit_price;
?>
<tr>
<td><input type="text" class="common quantity" name="1" id="quant" value="<?php echo $quantity; ?>"></td>
<td><input type="text" class="common price" name="2" id="units" value="<?php echo $unit_price; ?>"></td>
<td><input type="text" class="total" name="3" id="total" readonly></td>
</tr>
<?php } ?>
<tr>
<td colspan="2"><label class="form-control">Subtotal</label></td>
<td><input name="subtotal" readonly id="subtotal" class="sub" type="text" /></td>
</tr>
</table>
</form>
jQuery
<script>
$(".total").each(function() {
$('.total').val(parseFloat($('#quant').val()) * parseFloat($('#units').val()));
});
</script>
最佳答案
迭代行并使用类而不是 ID,否则它只取第一个匹配的:
$("#my-table tr:not(:last)").each(function() {
$(this).find('.total').val(parseFloat($(this).find('.quantity').val()) * parseFloat($(this).find('.price').val()));
});
});
关于php - jQuery 中两列的乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41974267/