1 回答

TA貢獻1802條經(jīng)驗 獲得超6個贊
我已將data-amount
and添加data-price
到您的輸入中,然后更改currbalance = amount - totalSum;
為currbalance += (amount - totalSum);
,因為如果不是currbalance
,則不等于data-amount
$('.form-group').on('input', '.prc', function() {
var totalSum = 0;
var currbalance = 0;
$('.form-group .prc').each(function() {
var quantity = $(this).val();
var price = $(this).attr('data-price');
var item_amount = parseFloat(price * quantity);
if ($.isNumeric(quantity)) {
totalSum += parseFloat(item_amount);
}
var amount = $(this).attr('data-amount');
if ($.isNumeric(amount)) {
currbalance += (amount - totalSum);
}
});
$('#total_price').text('RM ' + totalSum.toFixed(2));
$('#balance').text('RM ' + currbalance.toFixed(2));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<input style="width:200px" type="number" data-amount="120" data-price="12" class="form-control pull-right prc" placeholder="Enter amount">
<input style="width:200px" type="number" data-amount="220" data-price="22" class="form-control pull-right prc" placeholder="Enter amount">
</div>
<p>Total: <b id="total_price">RM 0.00</b></p>
<p>Balance: <b id="balance">RM 0.00</b></p>
- 1 回答
- 0 關注
- 122 瀏覽
添加回答
舉報