Javascript 如何检查数值是否大于ng更改时的数值
) 闪电战: 请检查: 我有一个数字Javascript 如何检查数值是否大于ng更改时的数值,javascript,angularjs,Javascript,Angularjs,) 闪电战: 请检查: 我有一个数字总费用和一个数据表 有一列Amt。该列包含预付款,通过在Amt旁边的输入框中输入,可以扣除预付款并将其支付到当前费用(1000) 正如您在第三列中看到的,我已经输入了500,现在我可以在第一行输入500(或者在第二行输入100,在第一行输入400) 在输入框的ng change上,我通过计算deductedTillNow和balanceleftinfense检查输入的金额是否大于从总费用中扣除的余额 您可以在chrome扩展中看到右侧的$scope变量dedu
总费用和一个数据表
有一列Amt
。该列包含预付款,通过在Amt
旁边的输入框中输入,可以扣除预付款并将其支付到当前费用(1000)
正如您在第三列中看到的,我已经输入了500,现在我可以在第一行输入500(或者在第二行输入100,在第一行输入400)
在输入框的ng change
上,我通过计算deductedTillNow
和balanceleftinfense
检查输入的金额是否大于从总费用中扣除的余额
您可以在chrome扩展中看到右侧的$scope
变量deductedTillNow
和BalanceLeftInEnsense
的值。当我试图通过输入50
从第一行减去剩余的500时,余额leftinEnse
变为450
因此,我的验证不允许输入下一个数字,因为我尝试在50
,0
之后输入下一个数字,余额leftInEnsense
为450
且500大于450。如何允许输入500
//calculate amount till now
$scope.deductedTillNow = 0;
for (let j = 0; j < $scope.amountLedgerListArray.length; j++) {
if($scope.amountLedgerListArray[j].adjustAmount){
$scope.deductedTillNow = $scope.amountLedgerListArray[j].adjustAmount + $scope.deductedTillNow;
}
}
//calculate balance left in expense
$scope.balanceLeftInExpense = $scope.totalExpenseAmount - $scope.deductedTillNow;
html:
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Payment Ref no</th>
<th>Date</th>
<th>Amt</th>
<th >Adjust amount</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in amountLedgerListArray">
<td>
{{item.payment_ref_no}}
</td>
<td>
{{item.payment_date}}
</td>
<td>
{{item.amount}}
</td>
<td>
<input type="number" name="sgst" class="form-control"
ng-disabled="item.isDisabled" autocomplete="off"
ng-change="calculateBalanceAndRestrict(item)"
ng-class="{'borderRedError' : expense.$invalid}"
ng-model="item.adjustAmount" onkeypress="limitKeyPress(this, 10, event)" />
</td>
</tr>
</tbody>
</table>
付款参考号
日期
金额
调整金额
{{项目.付款{参考号}
{{项目.付款日期}
{{item.amount}}
编辑:
“金额”列中的数据是已支付的预付款。现在有一个费用(本例中为1000。请参见屏幕截图总费用:1000
)。我可以从Amt
列中的任何行中扣除
这里我从第三行扣除了500英镑(现在的余额是500英镑)。我可以从最上面的两行中扣除剩下的500。第二行的100(因为它只有100)和第一行的剩余500,或者第一行的剩余500
我这样进行验证:在更改输入框时,我检查输入的金额是否大于输入的所有金额之和(deductedTillNow
)。当我现在开始在第一行输入时,会计算出费用中的余额。当我输入50时,左边的余额是450(1000-500(第三行)+50(第一行))
当我在50之后为第一行剩余的500键入0时,左边的余额是450。因此,我的验证不允许这样做(输入的500大于450)。我需要允许键入500。将您的if条件更改为以下条件:
if(($scope.totalExpenseAmount <= $scope.deductedTillNow) || (($scope.totalExpenseAmount - $scope.deductedTillNow) < item.adjustAmount) ){
item.adjustAmount = item.adjustAmount.toString();
item.adjustAmount = item.adjustAmount.substring(0, item.adjustAmount.length - 1);
item.adjustAmount = parseInt(item.adjustAmount);
}
if($scope.totalExpenseAmount代码中的主要问题是错误地检查了余额left
if(item.adjustAmount > item.amount || (item.amount <= 0)){
item.adjustAmount = null;
return
}
if(item.adjustAmount>item.amount ||(item.amount那么,当前代码有什么问题?@Justcode我的验证不允许在50之后为剩余的500输入0,因为当我键入50
时,余额现在是450。你能创建演示来重现问题吗?@Justcode我没有时间。我只需要10分钟的午休时间。我真的不能哦。你明白问题是什么吗?我在第三行第一次键入500后,尝试在第一行键入500。当我开始键入并键入5(小于495)时,BalanceLeftInEnsense
是495,当我键入0(小于450)时接下来,它是450。当我尝试键入500时,余额leftInEnsense
是450,但500大于余额leftInEnsense(450),因此我无法键入0。请参阅上面的我的子字符串()
验证如果我理解正确,您应该检查limitKeyPress
我不理解最后一部分“当帐户禁用代码时…”1.我想你已经粘贴了我的代码……我看不出最后一部分有什么不同
if(item.adjustAmount > item.amount || (item.amount <= 0)){
item.adjustAmount = null;
return
}
$scope.balanceLeftInExpense = $scope.totalExpenseAmount - $scope.deductedTillNow;
if ($scope.balanceLeftInExpense < 0) {
item.adjustAmount = item.adjustAmount.toString();
item.adjustAmount = item.adjustAmount.substring(0, item.adjustAmount.length - 1);
item.adjustAmount = parseInt(item.adjustAmount);
}
//if amount entered is equal to advance paid of that row, enable others
for (let i = 0; i < $scope.amountLedgerListArray.length; i++) {
if (Number($scope.amountLedgerListArray[i].amount) < $scope.amountLedgerListArray[i].adjustAmount) {
$scope.amountLedgerListArray[i].isDisabled = false;
}
else {
//disable those that dont have a value in it
if ($scope.amountLedgerListArray[i].advance_id != item.advance_id && !$scope.amountLedgerListArray[i].adjustAmount && Number($scope.amountLedgerListArray[i].amount) < $scope.amountLedgerListArray[i].adjustAmount) {
$scope.amountLedgerListArray[i].isDisabled = true;
}
}
}