0
I have a Jquery responsible for popular a select based on the value of another select, solved the issue successfully using the following code:
$("#tipoProcedimento").change(function(){
$("#procedimento").empty();
$("#procedimento").attr("disabled", true);
$.ajax('http://localhost:8000/backoffice/agendamentos/procedimentos-grupos/'+$(this).val()+'/procedimentos',
{
success: function (data)
{
console.log(data);
$("#procedimento").empty();
$("#procedimento").append($("<option />").val('').text('SEM PROCEDIMENTO ESPECIFICO...'));
$.each(data.procedimentos, function() {
$("#procedimento").append($("<option />").val(this.nr_seq_proc_interno).text(this.desc_procedimento_busca1));
});
$("#procedimento").attr("disabled", false);
},
error: function (data)
{
console.log('err -> '.data)
$("#procedimento").attr("disabled", true);
}
});
})
However, today I need to use the same code on another page for 4 different selects, which led me to the conclusion that the best way to do this, would be to transform the names of the respective selects into variables in order to NOT REPEAT CODE xD... So I would be able to call the function for each select as follows:
$("#tipoProcedimento").change(carregarProcedimentos('#procedimento','#tipoProcedimento'))
$("#select2").change(carregarProcedimentos('#selectSecundario2','#select2'))
$("#select3").change(carregarProcedimentos('#selectSecundario3','#select3'))
...
But when calling the function this second way, for some reason it is triggered as soon as the page loads, behavior of which did not happen in the previous way !!! And frankly, I’m confused to understand why this... I thought it had something to do with calling the function asynchronously, I couldn’t solve it this way either... so here I am (: