New or previous value in script according to input

1

I need that when "nuevo_saldo" equals zero: the value of "restan" is also zero. But ... if it were not like that, keep its initial value .

EDITED: Thanks to a timely response (@alanfcm) an advance was achieved in this problem, it only persists (possibly they call me fussy, but I think about the optimal way the end user should work) a small problem.

When the value of% co_of% is equal to zero because% co_of% succeeded in covering "restan" all would be fine . But really the end user (in one of his mistakes or changes) would have to be able to put another amount in "importe" and be able to perform the initial operation without it being the zero value that was generated by his "error or change".

Any idea how to do it? I still think about the creation of a new field "restan" that could cover this point, what do you think?

Thanks in advance.

$(document).ready(function() {
  function operacion_resta() {
    var uno, dos, tres, operacion;
    uno = $('#importe');
    dos = $('#restan');
    tres = $('#nuevo_saldo');
    operacion = parseFloat(dos.val()) - parseFloat(uno.val());
    tres.val(operacion);
    if (tres.val() == "0") {
      $('#restan').val('0');
    }
  }

  $("#importe").keyup(function() {
    var dos;
    dos = $('#restan').val();
    if (dos != "") {
      operacion_resta()
    }
  });

  $('#nuevo_saldo').change(function() {
    var dos, tres;
    tres = $('#nuevo_saldo').val();
    if (tres == "0") {
      dos = $('#restan').val('0');
    } else {

    }
  });

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" />

<div class="col-xs-2">
  <input type="text" id="restan" name="restan" value="800"><br>
  <label> Realmente es de tipo "hidden" pero para valores prácticos (ver resultados) aquí sera de tipo "text".</label>
</div>
<label>------------------------------------------------</label>
<div class="box-body">
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-2">
        <label for="importe">Importe: *</label>
        <input type="text" class="form-control" id="importe" placeholder="$0" name="importe" required>
      </div>

      <div class="col-xs-2">
        <label for="nuevo_saldo">Su nuevo saldo es:</label>
        <input type="text" class="form-control" id="nuevo_saldo" placeholder="$0" name="nuevo_saldo" readonly>
      </div>
    </div>
  </div>
</div>
    
asked by González 17.05.2018 в 17:28
source

1 answer

1

You can make the change in your operacion_resta () function like this:

$(document).ready(function() {
  function operacion_resta() {
    var uno, dos, tres, operacion;
    uno = $('#importe');
    dos = $('#restan');
    tres = $('#nuevo_saldo');
    operacion = parseFloat(dos.val()) - parseFloat(uno.val());
    tres.val(operacion);
    if (tres.val() == "0") {
      $('#restan').val('0');
    }
  }

  $("#importe").keyup(function() {
    var dos;
    dos = $('#restan').val();
    if (dos != "") {
      operacion_resta()
    }
  });
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" />

<div class="col-xs-2">
  <input type="text" id="restan" name="restan" value="800"><br>
  <label> Realmente es de tipo "hidden" pero para valores prácticos (ver resultados) aquí sera de tipo "text".</label>
</div>
<label>------------------------------------------------</label>
<div class="box-body">
  <div class="container-fluid">
    <div class="row">
      <div class="col-xs-2">
        <label for="importe">Importe: *</label>
        <input type="text" class="form-control" id="importe" placeholder="$0" name="importe" required>
      </div>

      <div class="col-xs-2">
        <label for="nuevo_saldo">Su nuevo saldo es:</label>
        <input type="text" class="form-control" id="nuevo_saldo" placeholder="$0" name="nuevo_saldo" readonly>
      </div>
    </div>
  </div>
</div>
    
answered by 17.05.2018 в 17:35