gpt4 book ai didi

javascript - HTML 表单输入 javascript 自动求和 支付总价

转载 作者:行者123 更新时间:2023-11-29 12:09:35 25 4
gpt4 key购买 nike

我有以下代码来销售产品,如果我应用折扣,它可以工作,如果我应用税,则同样,每单位的所有价格(数量 1)都有效,但如果我更改数量,则支付的总价不起作用

我需要的是自动填充以便稍后将以下字段保存到 mysql:

default price per unit (comming from mysql) = working
apply taxes or not per unit = working
apply discount per unit = working
price per unit after discount and after taxes = working

如果我将数量从 1 更改为 2 o 3 等,则需要支付的总价 = 不起作用这必须通过乘以每单位税后按数量支付的价格来自动填充=不起作用

这是代码

<table width="339" border="0" cellpadding="0">
<tr>
<td width="98">Pay taxes?</td>
<td width="115">Make Discount?</td>
<td width="118">Default unit price</td>
<td>Quantity</td>
</tr>
<tr>
<td>
<select name="taxes" class="select">
<option value="0" selected>no taxes</option>
<option value="19">19% taxes</option>
</select>
</td>
<td>
<select name="discount" class="select">
<option value="0" selected>no discount</option>
<option value="5">5% discount</option>
<option value="10">10% discount</option>
<option value="20">20% discount</option>
</select>
</td>
<td>
<input type="text" name="cost" class="input140" value="1000">
</td>
<td><input type="text" name="quantity" value="1"></td>
</tr>
<tr>
<td>Unit price after discount</td>
<td>Tax per unit</td>
<td>Total Price per unit</td>
<td>Total Price to pay per quantity</td>
</tr>
<tr>
<td><input type="text" name="price" value="1000"></td>
<td><input type="text" name="ttaxes" value="0"></td>
<td><input type="text" name="total" value="1000"></td>
<td><input type="text" name="totaltopay" value="1000"></td>
</tr>
</table><script>
/**
* Elements
*/
var taxes = document.getElementsByName('taxes')[0];
var discount = document.getElementsByName('discount')[0];
var cost = document.getElementsByName('cost')[0];
var price = document.getElementsByName('price')[0];
var ttaxes = document.getElementsByName('ttaxes')[0];
var total = document.getElementsByName('total')[0];


/**
* Calculations
*/
function updateInput() {
price.value = cost.value - (cost.value * (discount.value / 100));
ttaxes.value = (price.value * (taxes.value / 100));
var sum = parseFloat(price.value) + parseFloat(ttaxes.value);
total.value = sum.toFixed(0);
}


/**
* Event Listeners
*/
taxes.addEventListener('change', updateInput);
discount.addEventListener('change', updateInput);
cost.addEventListener('change', updateInput);
cost.addEventListener('keyup', updateInput);
</script>

这是演示 fiddle

https://fiddle.jshell.net/63dkvgs5/

我需要的图像示例

enter image description here

最佳答案

您忘记添加数量!

var quantity = document.getElementsByName('quantity')[0];
quantity.addEventListener('keyup', updateInput);

并更新计算:

var sum = (parseFloat(price.value) + parseFloat(ttaxes.value)) * quantity.value;

fiddle :https://fiddle.jshell.net/praveenscience/v6spxoqv/

关于javascript - HTML 表单输入 javascript 自动求和 支付总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30918840/

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