我正在尝试计算一个数字的百分比(取自一个文本字段)并将计算的值设置为另一个表单字段。为此,我尝试了以下代码,但我观察到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>
答案 0 :(得分:1)
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;
}
&#13;
<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>
&#13;