Enable and Disable Save Button

Asked

Viewed 2,239 times

7

I am developing a Revenue/Expenditure launch system and it works according to what I initially need.

Lawsuits.
Revenue Launch, add the Revenue value to the account table.
Example:
Opening account balance = 0,00
Revenue Launch = 100,00
Balance Account = 100,00

Expense Launch, subtract the Revenue value from the account table.
Example:
Opening Account Balance = 100,00
Expenditure Launch = 50,00
Balance Account = 50,00

Launch Charge, subtract or sum according to the type of launch (Revenue or Expense) and added value.
Example:
Estorno de Receita
Opening Balance = 100,00
Chargeback Revenue = 100,00
Balance Account = 0,00

Chargeback
Opening Balance = 100,00
Chargeback Revenue = 100,00
Balance Account = 200,00

The problem is when I click edit the release, do not change anything in the register and save again. Just because I have clicked on Save, The Update balance calculation is done again, when I should not update anything, since I have not changed any checkbox.

Therefore, there is some way to disable the button and enable only when there is indeed some change in the fields(A way to validate between the values before and after)?

So the button SAVE CHANGES will only be available if the person actually changes something in the modal.

inserir a descrição da imagem aqui

Or, if there is any other option, it would be of great help.

  • This system is based on maps? I have a lot of stuff that I developed based on it, we can exchange some ideas.

  • @Andrébaill Yes, too! !!

  • Ok, what’s your contact? or if you want, add me on skype: srandrebaill :)

2 answers

8


When you edit:

$('form :input').change(function(){
   alert("Algum item no formulário foi alterado");
   //A partir daqui vocÊ pode habilitar o botão
   //Para desabilitar $('#idDoBotao').attr('disabled', true);
   //Para habilitar $('#idDoBotao').attr('disabled', false);
});
  • Ball show, it worked.. Help until I managed to develop something, I will use your code. The ideal would be; whenever I go back to the initial state (the same state as when I opened the modal) the button goes back to the disabled state again. That is, remain enabled if actually the values were changed, different from what is already registered.

6

Using the reply from @ggui as a basis, you can save the field value with the event .phocus() and by .change() or any other event you are using, you perform the check whether the value is equal or not. It would look something like this:

$(document).ready(function() {
  var valorOriginal;

  $('form :input').focus(function() {
    valorOriginal = $(this).val();
  }).change(function() {
    var novoValor = $(this).val();

    if (valorOriginal != novoValor) {
      $('#btn-salvar').attr('disabled', false)
    } else {
      $('#btn-salvar').attr('disabled', true)
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#">
  <div>
    <p>
      Descrição
    </p>
    <input type="text" value="Receita" />
  </div>
  <div>
    <p>
      Fornecedor
    </p>
    <input type="text" value="Wagner" />
  </div>
  <div>
    <p>
      Valor
    </p>
    <input type="text" value="250.00" />
  </div>
  <br/>
  <input type="button" value="Salvar Alterações" id="btn-salvar" disabled="disabled" />
</form>

You could also use the .date-* to store the old value and perform the check if the value was changed or not. It would look something like this:

$(document).ready(function() {
  $('form :input').change(function() {
    if ($(this).val() != $(this).data('valor')) {
      $('#btn-salvar').attr('disabled', false)
    } else {
      $('#btn-salvar').attr('disabled', true)
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#">
  <div>
    <p>
      Descrição
    </p>
    <input type="text" value="Receita" data-valor="Receita" />
  </div>
  <div>
    <p>
      Fornecedor
    </p>
    <input type="text" value="Wagner" data-valor="Wagner" />
  </div>
  <div>
    <p>
      Valor
    </p>
    <input type="text" value="250.00" data-valor="250.00" />
  </div>
  <br/>
  <input type="button" value="Salvar Alterações" id="btn-salvar" disabled="disabled" />
</form>

This second way you have the persistence of the original value all the time, different from the first way.

You can also do several other ways, such as localStorage, cookies, hiddens, etc...

However, client validations may not be the best solution if you don’t have server validation as well. So I think it’s best to ensure validation on the server and then think about client validation.

  • I even tried to use it the way it did, but it didn’t work the way I thought it would. Could use cookies, localStorage. But I don’t know how.

  • @Wagnerfilho Which of the two ways did not work as expected?

  • I did it both ways. The first one presses the button with the disabled = true attribute. If I change any field, the button becomes disabled = false(so far, so good). But if I return the field changed to the source value, the button is still enabled. The second option didn’t work.

  • @Wagnerfilho As I said in the answer, the first form has this "problem". The second one should work. If you click on the "run" button in my reply, you will see that it is working normally now (I forgot to add jQuery earlier)

  • @Wagnerfilho I must inform you that I am using the event .change(), then it does not check each key pressed. If you want to validate by key, change the change for keyup which will work (in inputs)

  • Nice example, that’s exactly what it would be. Only I haven’t had the evil to apply in my code yet. Look at my input: <input class="span12" id="descricaoEditar" type="text" name="descricao" /> While the value of this field is assigned by $("#descricaoEditar").val($(this).attr('descricao')); So I don’t know how to apply the data-valor.

  • @Wagnerfilho attr(descricao) does not exist. If you follow the example you need to create the data-descricao and the value must be recovered with the $("#descricaoEditar").val($(this).data('descricao')); Something like that

  • Excuse me, your code is amazing, but it did not (For lack of knowledge really). : <input class="span12" id="descricaoEditar" type="text" name="descricao" data-descricao="" /> $('form :input').change(function() {&#xA; if ($(this).val() != $(this).data('descricao')) {&#xA; $('#btn-salvar').attr('disabled', false)&#xA; } else {&#xA; $('#btn-salvar').attr('disabled', true) $(document).on('click', '.editar', function(event) {&#xA; $("#idEditar").val($(this).attr('idLancamento'));&#xA; $("#descricaoEditar").val($(this).data('descricao'))

  • I only did it with the description field in order to test it. But thank you so much for the strength, since I learned something else...

  • @Wagnerfilho I made a change to your code. See if this example helps you understand.

  • My problem is how to recover the data-value from the database in the same way as it is recovered in value. Example: value="valor recuperado" data-valor="valor recuperado" From there on, you could do what the code proposes.

  • @Wagnerfilho Well, there is already subject for another question. xD

  • I got you right?! rsrs. Thanks for the return. Anyway, now I have a direction about what I wanted...

Show 8 more comments

Browser other questions tagged

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