猿问

如何使用javascript将之前的余额添加到当前余额

PHP


<div class="col-md-2">

        Amount<input type="text" name="edit_amount" id="edit_amount" class="form-control" onkeypress="isInputNumber(event)"/>

        </div>



        <div class="col-md-2">

        Cash Tendered</strong>: <input type="text" name="edit_payment" id="edit_payment" class="form-control"onkeypress="isInputNumber(event)"/>

        </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="<?php echo $row['balance'] ?>" />

        </div>

该行余额是患者最后一笔交易的余额。此回波平衡可查看/检查患者的剩余余额。


JavaScript


$(function() {


    $("#edit_payment").on("keydown keyup", sum);

 function sum() {


    var textValue1 = Number.parseFloat(document.getElementById('edit_payment').value);

    var textValue2 = Number.parseFloat(document.getElementById('edit_amount').value);

   if (textValue1>=textValue2) {

    document.getElementById('edit_balance').value = "0"; 

    document.getElementById('edit_change').value = textValue1 - textValue2; 

   }

    else if(textValue1<textValue2){

      document.getElementById('edit_change').value = "0"; 

      document.getElementById('edit_balance').value = textValue2 - textValue1; 


    }

 }


});

我的问题是,如果患者再次进行交易,并且患者将有另一个余额,我想添加之前的余额和当前余额。


例如,


患者在上次交易中的余额为 100 美元 然后患者将再次进行交易,交易总额为 200 美元,然后患者支付 50 美元,因此余额为 150 美元。那么该患者的总余额将变为 250 美元,因为之前的 100 美元然后当前的 150 美元。


DIEA
浏览 90回答 1
1回答

一只甜甜圈

您的代码中有两个问题:isInputNumber已绑定,#edit_payment因此 jQuery 回调将无法正常工作。如果你想减少每个keyup事件的余额,你必须将第一个balance(&nbsp;$row['balance'])存储在事件监听器之前的某个地方。const parseNumber = function(value) {&nbsp; &nbsp;value = parseFloat(value);&nbsp; &nbsp;if (isNaN(value)) {&nbsp; &nbsp; &nbsp; value = 0;&nbsp; &nbsp;}&nbsp; &nbsp;return value;}const balanceInput = $("#edit_balance");const paymentInput = $("#edit_payment");const balance = parseNumber(balanceInput.val());$("#edit_payment").on("keyup", function() {&nbsp; let tmpBalance = balance;&nbsp;&nbsp;&nbsp; let amount = parseNumber(paymentInput.val());&nbsp; if (tmpBalance >= amount) {&nbsp; &nbsp; &nbsp;tmpBalance = tmpBalance - amount;&nbsp; }&nbsp; balanceInput.val(tmpBalance);});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="col-md-2">&nbsp; &nbsp; &nbsp; &nbsp; Amount<input type="text" name="edit_amount" id="edit_amount" class="form-control" />&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-2">&nbsp; &nbsp; &nbsp; &nbsp; Cash Tendered</strong>: <input type="text" name="edit_payment" id="edit_payment" class="form-control"/>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="col-md-2">&nbsp; &nbsp; &nbsp; &nbsp;Change </strong>: <input type="text" name="edit_change" id="edit_change"readonly="" class="form-control"/>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="col-md-2">&nbsp; &nbsp; &nbsp; &nbsp; Balance</strong>: <input type="text" name="edit_balance" id="edit_balance"readonly="" class="form-control" value="40" />&nbsp; &nbsp; &nbsp; &nbsp; </div>
随时随地看视频慕课网APP

相关分类

Html5
我要回答