如何在HTML输入类型编号中访问和更新值?
我正在尝试执行一个计算,其中用户在第一个输入中输入的数字立即返回在ReadOnly第二输入字段中。这是一个简单的计算,要占第一个数字的20%并将其添加到第一个数字中,将在第二个数字字段中返回。 (例如,$ 100的20%转移费用将返回$ 120。
我似乎可以访问输入字段中的值,但是返回的计算没有返回正确的值。如果在第一个输入中输入40个,则返回的第二个输入显示408而不是48。有人可以帮忙吗?
function myFunc() {
const transferamount = document.getElementById("sendamount").value;
document.getElementById("totalled").value = transferamount + (0.20 * transferamount);
}
<form>
<input type="number" id="sendamount" value="" name="amounttosend" oninput="myFunc()" required>
<input type="text" id="totalled" value="" readonly>
</form>
I am trying to perform a calculation where the number entered by the user in the first input is returned in the readonly second input field instantly. Its a simple calculation to take 20% of the first number and add it back to the first number, to be returned in the second number field. (e.g 20% transfer fees on $100 would return $120.
It seems I am able to access the values in the input fields, but the returned calculation is not returning the correct values. If 40 is entered in the first input, the returned second input displays 408 instead of 48. Can someone help?
function myFunc() {
const transferamount = document.getElementById("sendamount").value;
document.getElementById("totalled").value = transferamount + (0.20 * transferamount);
}
<form>
<input type="number" id="sendamount" value="" name="amounttosend" oninput="myFunc()" required>
<input type="text" id="totalled" value="" readonly>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从输入中获得的价值始终被视为字符串,这就是为什么计算不正确的原因。要按预期执行,您需要使用parseint()或parsefloat()将输入转换为int或float
the value you get from an input is always considered as a string, that's why your calculations are incorrect. To perform as expected you need to convert your input to an int or a float using parseInt() or parseFloat()