gpt4 book ai didi

javascript - 百分比计算错误

转载 作者:搜寻专家 更新时间:2023-10-31 08:52:44 27 4
gpt4 key购买 nike

我正在尝试计算数字的百分比(取自一个文本字段)并将计算值设置到另一个表单字段。为此,我尝试了以下代码,但我观察到 js 正在检测的关键事件/小数位不匹配。

例如,如果我在第一个输入字段中提供 100(以计算 1.75 的百分比),根据我的 js 代码,我预计为 1.75,但在第二个文本字段中它为 0.175。我知道这是一个非常简单的计算,但我无法解决这个问题。

var reFee = document.getElementById("Estimated Referral Fee");
var LnAmt = document.getElementById("Loan Amount");
reFee.disabled = true;

LnAmt.onkeydown =
function isNumber(evt) {

evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
calcPer(LnAmt.value);
}

function calcPer(amtval) {
var pernum = 1.75;
reFee.value = (pernum / 100) * amtval;
}
<div class="form-group">
<label class="control-label col-sm-2">Loan Amount</label>
<div class="col-sm-10">
<input id="Loan Amount" placeholder="Provide Loan Amount" data-toggle="tooltip" data-original-title="Provide Loan Amount" required="required" class="form-control" name="Loan Amount" type="text" value="">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Estimated Referral Fee</label>
<div class="col-sm-10">
<input id="Estimated Referral Fee" placeholder="Your referral fee" data-toggle="tooltip" data-original-title="Your referral fee" required="required" class="form-control" name="Estimated Referral Fee" type="text" value="" disabled="">
<span class="help-block"></span>
</div>
</div>

最佳答案

var reFee = document.getElementById("Estimated Referral Fee");
var LnAmt = document.getElementById("Loan Amount");
reFee.disabled = true;

LnAmt.onkeyup =
function isNumber(evt) {

evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
calcPer(LnAmt.value);
}

function calcPer(amtval) {
var pernum = 1.75;
reFee.value = (pernum / 100) * amtval;
}
<div class="form-group">
<label class="control-label col-sm-2">Loan Amount</label>
<div class="col-sm-10">
<input id="Loan Amount" placeholder="Provide Loan Amount" data-toggle="tooltip" data-original-title="Provide Loan Amount" required="required" class="form-control" name="Loan Amount" type="text" value="">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Estimated Referral Fee</label>
<div class="col-sm-10">
<input id="Estimated Referral Fee" placeholder="Your referral fee" data-toggle="tooltip" data-original-title="Your referral fee" required="required" class="form-control" name="Estimated Referral Fee" type="text" value="" disabled="">
<span class="help-block"></span>
</div>
</div>

关于javascript - 百分比计算错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37116437/

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