gpt4 book ai didi

带有文本框的javascript动态乘法

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

我试图在文本框中进行动态乘法运算。现在我只想做添加。请看看我的js代码。我想将这 2 个值相乘。

这是我的代码:

<form name="submitform">
<table>
<tr id="service">
<td>
<span>Amount:</span>
</td>
<td>
<input type="text" name="amount" class="amount" onkeyup="onkeyupsum()" autocomplete="off" />
</td>
<td>
<span>Width: </span>
</td>
<td>
<input type="text" name="amount" class="amount" onkeyup="onkeyupsum()" autocomplete="off" />
</td>
</tr>

<tr>
<td>
<h4>Total Amount</h4>
</td>
<td>
<input type="text" name="tamt" id="tamt" />
</td>
</tr>
</table>
</form>

JavaScript 代码(HTML 格式):

function onkeyupsum() { // calculate sum and show in textbox
var sum = 0,
amount = document.querySelectorAll('.amount'), i;
for (i = 0; i < amount.length; i++) {
sum += parseFloat(amount[i].value || 0);
}
document.submitform.tamt.value = sum;
}

最佳答案

在变量初始化中设置 multiply = 1,并使用 multiply *= parseFloat(amount[i].value || 1); 计算乘法值:

function onkeyupsum() { // calculate sum and show in textbox
var multiply = 1,
amount = document.querySelectorAll('.amount'), i;
for (i = 0; i < amount.length; i++) {
multiply *= parseFloat(amount[i].value || 1);
}
document.submitform.tamt.value = multiply;
}
<form name="submitform">
<table>
<tr id="service">
<td> <span>Amount:</span>
</td>
<td>
<input type="text" name="amount" class="amount" onkeyup="onkeyupsum()" autocomplete="off" />
</td>

<td> <span>Width: </span>
</td>
<td>
<input type="text" name="amount" class="amount" onkeyup="onkeyupsum()" autocomplete="off" />
</td>
</tr>

<tr><td>
<h4>Total Amount</h4>
</td><td>

<input type="text" name="tamt" id="tamt" />
</td></tr>

</table>
</form>

关于带有文本框的javascript动态乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50289901/

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