如果金额验证的条件,javascript中的问题。 [英] Problem in javascript if condition of amount validation .
问题描述
我有以下javascript代码
,当我检查值
I have following javascript code
when i check the value
if (amountValue >= memberDueValue)
它转到其他条件但是如果条件值应该是
it goes to else condition but should be in if condition the values are
amountValue
=
220000
memberDueValue
=
50023.14
如果
if
amountValue
>
memberDueValue
且
amountValue
的第一个值是小于
memberDueValue
的第一个值。
它总是转到其他地方。
我的尝试:
.
it always goes to else.
What I have tried:
var memberDue = document.getElementById("lblTotalDueAmount_" + typeId + "_" + memberId).value;
var savingBalance = document.getElementById("lblMemberSavingAmount_" + typeId + "_" + memberId).value
var amount = document.getElementById("lblMemberTotal_" + typeId + "_" + memberId).value;
var memberDueValue = parseFloat(memberDue).toFixed(2);
var savingBalanceValue = parseFloat(savingBalance).toFixed(2);
var amountValue = parseFloat(amount).toFixed(2);
alert(amountValue + ", " + memberDueValue + ", " + (memberDueValue - amountValue).toFixed(2));
if (amountValue >= memberDueValue) {
document.getElementById("lblMemberDepositAmount_" + typeId + "_" + memberId).value = (amountValue - memberDueValue).toFixed(2);
}
else {
var diff = (memberDueValue - amountValue).toFixed(2);
if (diff > savingBalanceValue) {
document.getElementById("lblMemberWithdrawlAmount_" + typeId + "_" + memberId).value = savingBalanceValue;
}
else {
document.getElementById("lblMemberWithdrawlAmount_" + typeId + "_" + memberId).value = (memberDueValue - amountValue).toFixed(2);
}
}
推荐答案
您好:
我认为问题在于:
Hello:
I think that the problem is in the lines:
var savingBalanceValue = parseFloat(savingBalance).toFixed(2);
var amountValue = parseFloat(amount).toFixed(2);
如果它们是:
if they will be:
var savingBalanceValue = parseFloat(savingBalance);
var amountValue = parseFloat(amount);
逻辑将正常工作。
当你使用
the logic will work correctly.
When you use
.toFixed(2)
时,结果是:
the result of the asigment is that:
savingBalanceValue="220000";<br />
amountValue="50023.14"
因为 .toFixed 将结果转换为字符串,然后if中的比较不是数字之间的比较,而是字符串之间的比较然后
because .toFixed converts the result to string and then the comparation in the if is not a comparation between numbers but a comparation betwen strings and then
if (amountValue >= memberDueValue)
是
is
if ("220000" >= "50023.14")
转到其他条件。
that goes to the else condicion.
这篇关于如果金额验证的条件,javascript中的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!