Check if select field has changed

Asked

Viewed 1,445 times

1

I would like to create a js function, to check if a select field of my form has been changed. I created the function below to check if an input has been changed and works, but it does not work to select.

var alterado = false;
  $(document).ready(function(e) {
    $('input').keypress(function(){
      alterado = true;
    })
  });

2 answers

3


Try it like this:

$('#select').change(function(){
    alterado = true;
});

What I did: I had a function jQuery which will spy on a particular element specified by id, as soon as the function within the change will be executed.

  • It worked. Thank you.

0

<select name="formaPagamento" id="formaPagamento">
<option value="0">Selecione...</option>
<option value="D">Dinheiro</option>
<option value="C">Cartão</option>
<option value="DC">Dinheiro / Cartão</option>
</select>


var formaPagamento = $("#formaPagamento :selected").val();
if (formaPagamento == "0"){
// NÃO CLICOU
}

Browser other questions tagged

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