1 回答
TA贡献1836条经验 获得超5个赞
您的代码中有两个问题:
isInputNumber
已绑定,#edit_payment
因此 jQuery 回调将无法正常工作。如果你想减少每个
keyup
事件的余额,你必须将第一个balance($row['balance']
)存储在事件监听器之前的某个地方。
const parseNumber = function(value) {
value = parseFloat(value);
if (isNaN(value)) {
value = 0;
}
return value;
}
const balanceInput = $("#edit_balance");
const paymentInput = $("#edit_payment");
const balance = parseNumber(balanceInput.val());
$("#edit_payment").on("keyup", function() {
let tmpBalance = balance;
let amount = parseNumber(paymentInput.val());
if (tmpBalance >= amount) {
tmpBalance = tmpBalance - amount;
}
balanceInput.val(tmpBalance);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-2">
Amount<input type="text" name="edit_amount" id="edit_amount" class="form-control" />
</div>
<div class="col-md-2">
Cash Tendered</strong>: <input type="text" name="edit_payment" id="edit_payment" class="form-control"/>
</div>
<div class="col-md-2">
Change </strong>: <input type="text" name="edit_change" id="edit_change"readonly="" class="form-control"/>
</div>
<div class="col-md-2">
Balance</strong>: <input type="text" name="edit_balance" id="edit_balance"readonly="" class="form-control" value="40" />
</div>
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报