onchange Select does not work - jQuery

0
Hello everyone, I am doing onchange in the select tag but it is not working, I am using the JS Google plugin for cities and states, for example:

JSFIDDLE: link

    
asked by anonymous 28.12.2015 / 19:29

3 answers

2

Your script cidadesEstados.js is setting the .onchange() event to the following excerpt:

this.estado.onchange=function(){this.dgCidadesEstados.run()};

This is what you defined in the HTML is overwritten, so I advise you to use an eventListener:

var model = {};
document.addEventListener("readystatechange", function () {
  if (document.readyState == "interactive") {
    model = {
      estado: document.getElementById('estado'),
      cidade: document.getElementById('cidade'),
      change: true
    };    
    new dgCidadesEstados(model);
    
    //preste atenção no addEventListener;
    model.estado.addEventListener("change", myfunction);
  }
});

var myfunction = function (event) {
  alert(event);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><scriptsrc="http://ffsolucoesweb.com/ferramentas/cidadesEstados.js"></script>
<label>Estado</label>
<input id="estado" name="estado">
<label>Cidade</label>
<input id="cidade" name="cidade">

This way you can have more than one event occurring in the change of select state.

I also advise you to modify cidadesEstados.js to use eventListener whenever possible:

this.estado.addEventListener("change", function(event) { 
    this.dgCidadesEstados.run()
};
    
28.12.2015 / 19:45
1

Try adding change with jquery:

$(document).ready(function(){ $("#estado").change(function(){ alert(123); }); });

    
28.12.2015 / 19:40
1

The event .onchange is working correctly.

It is being assigned at line 91 of file cidadesEstados.js

this.estado.onchange= function() { this.dgCidadesEstados.run() };

In HTML you try to assign the myfunction()" function in the onchange event, you can only add one function per event (but you can call multiple functions inside one). If you want to leave your function in the onchange, I recommend refactoring the code and creating a select because the onchange event does not exist in the text field.

    
28.12.2015 / 19:43