Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store final value after being changed jQuery

How do I store the final value of balance amount that is being input?

HTML

<div id="balance_amount">
    balance amount: 
    <input type="number" id="balance-amount-input" name="balance_amount" value='+balance_amount+'>
</div>

JS

$('body').on('input','#balance-amount-input', function() {
    $('.jconfirm #error-msg').hide();
    var balance_amount_tmp=$(this).val();
    $('#balance-amount-input').val(balance_amount_tmp);
    if ((balance_amount_tmp-account_balance) > 0) {
        $('.jconfirm #error-msg').show();
        $('.jconfirm #error-msg').html('<p class="alert alert-warning">not enough</p>');
        $('.jconfirm-buttons button.btn-success').attr('disabled', 'true');
        return;
    } else {
        offline_amount = (total_amount - balance_amount_tmp).toFixed(2);
        $('#offline_amount').html(offline_amount);
        $('.jconfirm-buttons button.btn-success').removeAttr('disabled');
        return;
    }
});

Please help me to solve this!

like image 217
unknown Avatar asked Oct 29 '18 09:10

unknown


1 Answers

You can add another hidden input field to pass additional information with the form, or store this information into a variable and add it to the ajax call directly. for example you can use this:

$('body').on('input','#balance-amount-input', function() {
  $('.jconfirm #error-msg').hide();
  var balance_amount_tmp=$(this).val();
 // $('#balance-amount-input').val(balance_amount_tmp); //PTK: you don't need this line
  if ((balance_amount_tmp-account_balance) > 0) {
    $('.jconfirm #error-msg').show();
    $('.jconfirm #error-msg').html('<p class="alert alert-warning">用户余额不足</p>');
    $('.jconfirm-buttons button.btn-success').attr('disabled', 'true');
    return;
  } else {
    offline_amount = (total_amount - balance_amount_tmp).toFixed(2);
    $('#offline_amount').val(offline_amount); //PTK: I changed here html  to val
    $('.jconfirm-buttons button.btn-success').removeAttr('disabled');
    return;
  }
});
<div id="balance_amount">
  balance amount: 
  <input type="number" id="balance-amount-input" name="balance_amount" value='+balance_amount+'>
  <input type="hidden" id="offline_amount" name="offline_amount" value="">
</div>
like image 159
Pati Avatar answered Oct 17 '22 01:10

Pati