gpt4 book ai didi

php - 我想在更改数量时计算总金额

转载 作者:搜寻专家 更新时间:2023-10-31 21:23:41 26 4
gpt4 key购买 nike

我想在更改数量时获得总金额。

此脚本在 while 循环中不起作用:(数量 * 金额 = total_amount)

我该如何解决这个问题?请帮我解决这个问题。

function calculateTotal() {
var totalAmt = document.addem.total.value;
totalR = eval(totalAmt * document.addem.tb1.value);

document.getElementById('total_amount').innerHTML = totalR;
}
<table>
<tr>
<td>Id</td>
<td>Product</td>
<td>Quantity</td>
<td>Total</td>
</tr>
<tr>
<?php
$sq=mysql_query("select * from cart_sample where email='$ema'");
while($row = mysql_fetch_array($sq))
{ ?>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['product']; ?></td>

<form name="addem" id="addem" />
<td><input type="text" name="tb1" onkeyup="calculateTotal()" value="<?php echo $row['quantity']; ?>" /></td>
<input type="hidden" name="total" value="<?php echo $row['amount']; ?>" />
<td><span id="total_amount"></span></td>
<?php } ?> </tr>
</table>

最佳答案

由于您使用的是 jquery,因此您可以避免内联事件并为您的输入提供一个通用类,然后为所有输入创建一个事件:

<input type="text" class='quantity' name="tb1" onkeyup="calculateTotal()" value="<?php echo $row['quantity']; ?>" />
<input type="hidden" class='total' name="total" value="<?php echo $row['amount']; ?>" />
<span class="total_amount"></span>

JS :

$('.quantity').on('input', function(){
var form = $(this).closest('form');
var totalAmt = parseInt(form.find('.total').val());
var quantity = parseInt($(this).val());

form.find('.total_amount').text(totalAmt*quantity);
})

注意:表单应该在相同的 td 中。

希望这对您有所帮助。

$('.quantity').on('input', function(){
var form = $(this).closest('form');
var totalAmt = parseInt(form.find('.total').val());
var quantity = parseInt($(this).val());

form.find('.total_amount').text(totalAmt*quantity);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="text" class='quantity' name="tb1" value="1" />
<input type="hidden" class='total' name="total" value="10" />
<span class="total_amount">10</span>
</form>

<form>
<input type="text" class='quantity' name="tb1" value="2" />
<input type="hidden" class='total' name="total" value="10" />
<span class="total_amount">20</span>
</form>

<form>
<input type="text" class='quantity' name="tb1" value="3" />
<input type="hidden" class='total' name="total" value="10" />
<span class="total_amount">30</span>
</form>

没有表格:

$('.quantity').on('input', function(){
var parent = $(this).closest('tr');
var totalAmt = parseInt(parent.find('.total').val());
var quantity = parseInt($(this).val());

parent.find('.total_amount').text(totalAmt*quantity);

calcul_total_quatities();
})

function calcul_total_quatities()
{
var total = 0;
$('.total_amount').each(function(){
total += parseInt( $(this).text() );
})
$('.total_all_amounts').text(total);

post_data_to_server($('.total_amount').val(),total);
}


function post_data_to_server(total_amount,total_all_amounts)
{
$.ajax({
type: 'post',
url: 'your_page.php',
data: {
total_amount: total_amount,
total_all_amounts: total_all_amounts
},
success: function( data ) {
//'data' represent the message back from the PHP page

console.log( data );
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>Id</td>
<td>Product</td>
<td>Quantity</td>
<td>Total</td>
</tr>
<tr>
<td>ID</td>
<td>Product</td>
<td>
<input type="text" class='quantity' name="tb1" value="1" />
</td>
<td><input type="hidden" class='total' name="total" value="10" /></td>
<td><span class="total_amount">10</span></td>
</tr>
<tr>
<td>ID</td>
<td>Product</td>
<td>
<input type="text" class='quantity' value="2" />
</td>
<td><input type="hidden" class='total' value="10" /></td>
<td><span class="total_amount">20</span></td>
</tr>
<tr>
<td>ID</td>
<td>Product</td>
<td>
<input type="text" class='quantity' value="3" />
</td>
<td><input type="hidden" class='total' value="10" /></td>
<td><span class="total_amount">30</span></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td><span class="total_all_amounts">60</span></td>
</tr>
</table>

关于php - 我想在更改数量时计算总金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40974643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com