Select Change event does not work on Onload

0

I have the following Javascript function:

function Ini_Form() {
      var data="";
      data= '<option value="0">Seleccione...</option>';
      data= data + '<option value="2">TRANSPORTES MARVISUR</option>';
      $("#cmbResponsable").html(data);
      $('#cmbResponsable > option[value="2"]').attr('selected','selected');
      $("#cmbResponsable").change();
  }

This function think PHP (Select content is dynamic) and invoke the onload event of the page, the issue is that this select (#CmbResposable) I have tied to a select (#CmbDocumentos) loading some documents according to the responsible person that I select. so I need to execute the Change event of the select (#CmbResposable). Thank you in advance for your help and attention.

    
asked by Emersoft 06.11.2017 в 06:09
source

1 answer

0

You must create a Listener for the change event of the select, like this:

function Ini_Form() {

    var data="";
    data= '<option value="0">Seleccione...</option>';
    data= data + '<option value="2">TRANSPORTES MARVISUR</option>';
    $("#cmbResponsable").html(data);
    $('#cmbResponsable > option[value="2"]').attr('selected','selected');
    $("#cmbResponsable").change();
    $(document).on('change', "#cmbResponsable", function (e) {
        alert("ud ha seleccionado: "+$('#cmbResponsable').val()+" "+$('#cmbResponsable option:selected').text());
    });
}

Ini_Form();

As you are trying to create it does not work, because the DOM does not exist at the moment where line $("#cmbResponsable").change(); is executed, that is, it does not assign the event change(); to anything, because the element html #cmbResponsable it does not exist at that time.

Functional example

    
answered by 06.11.2017 / 06:31
source