Allow editing of input

Asked

Viewed 51 times

1

When selecting the form of payment, the default rate is filled in, but this rate must allow editing it.

I need to maintain the structure of my function, but at the same time allow the rate to be changed.

$(document).ready(function() {

  $(".moeda").maskMoney({decimal: ",",thousands: "."});

  $('#valorCurso, #formaPagamento, #formaTaxas, #poloComissao').bind('focusout change', function() {

    var formaID = $("#formaPagamento option:selected").val();
    var formaTxt = $("#formaPagamento option:selected").text();
    var formaTaxa = $("#formaPagamento option:selected").attr('data-taxa');
    var formaTipo = $("#formaPagamento option:selected").attr('data-tipo');


    if (formaID == "") {
      var taxas = $("#formaTaxas").val("0.00");
    } else {
      var taxas = $("#formaTaxas").val(formaTaxa + formaTipo);
    }



  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>

Forma de pagamento:
<select id="formaPagamento" name="formaPagamento" required>
  <option value="" selected></option>
  <option value="1" data-taxa="3.50" data-tipo="">Boleto</option>
  <option value="2" data-taxa="3.50" data-tipo="%">Cartão credito</option>
  <option value="3" data-taxa="1.50" data-tipo="%">Cartão debito</option>
  <option value="4" data-taxa="0.00" data-tipo="">Dinheiro</option>
</select>

Taxa: 
<input id="formaTaxas" name="formaTaxas" type="text" class="form-control moeda" value="0,00" required/>

  • Remove the selector #formaTaxas event does not solve the problem?

  • @Victorcarnaval No, because the intention would be that when finished changing the value would already recalculate everything automatically.

1 answer

1


Create another Event Handler with the event input only to the countryside #formaTaxas. Changing the field value will also change the attribute value data-taxa active option in select:

$('#formaTaxas').bind('input', function() {
   $("#formaPagamento option:selected").attr("data-taxa", this.value);
});

See in operation:

$(document).ready(function() {

   $(".moeda").maskMoney({decimal: ",",thousands: "."});

   $('#formaTaxas').bind('keyup', function() {
      $("#formaPagamento option:selected").attr("data-taxa", this.value);
   });
   
   $('#valorCurso, #formaPagamento, #formaTaxas, #poloComissao').bind('focusout change', function() {
   
      var formaID = $("#formaPagamento option:selected").val();
      var formaTxt = $("#formaPagamento option:selected").text();
      var formaTaxa = $("#formaPagamento option:selected").attr('data-taxa');
      var formaTipo = $("#formaPagamento option:selected").attr('data-tipo');
   
      if (formaID == "") {
         var taxas = $("#formaTaxas").val("0.00");
      } else {
         var taxas = $("#formaTaxas").val(formaTaxa + formaTipo);
      }
   });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
Forma de pagamento:
<select id="formaPagamento" name="formaPagamento" required>
  <option value="" selected></option>
  <option value="1" data-taxa="3.50" data-tipo="">Boleto</option>
  <option value="2" data-taxa="3.50" data-tipo="%">Cartão credito</option>
  <option value="3" data-taxa="1.50" data-tipo="%">Cartão debito</option>
  <option value="4" data-taxa="0.00" data-tipo="">Dinheiro</option>
</select>

Taxa: 
<input id="formaTaxas" name="formaTaxas" type="text" class="form-control moeda" value="0,00" required/>

  • Hello Sam, he is forcing me to use %. Even using a form that uses %, if I want to put a value without %, the field should allow, the opposite would also be valid. The fee field would be free to use with or without %. OBS: I edited my post because I forgot to put the script that formats the input.

  • So just change the event input for keyup. And you can remove replace tb that you don’t need. That’s the code: $('#formaTaxas').bind('keyup', function() {&#xA; $("#formaPagamento option:selected").attr("data-taxa", this.value);&#xA; });

  • Simblo does not leave when it has, or does not let put when it does not have.

  • maskmoney blocks all characters other than number.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.