Check if select field changed

1

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

var alterado = false;
  $(document).ready(function(e) {
    $('input').keypress(function(){
      alterado = true;
    })
  });
    
asked by anonymous 20.07.2015 / 16:04

2 answers

3

Try this:

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

What I did: I atrelo a jQuery function that will be spying on a certain element specified by the id, as soon as the function within the change is changed.

    
20.07.2015 / 16:09
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
}
    
20.07.2015 / 17:17