Instruction jQuery only works by console

Asked

Viewed 167 times

0

Hello, I have two instructions on jQuery that I use for:

  • Mark a checkbox
  • Disable a radio Buttons.

The point is that these instructions run by the system does not work, but if I take the same and play on the browser console everything happens correctly

jQuery

$.ajax({
    url: addressWebService+'auditoriaexcecao/getAuditorias/audi_id/'+audi_id,
    type: 'get',
    dataType: 'json'
}).done(function(r){
    if(r.length >= 1){
        var hora;
        $.each(r, function(i, obj){

            hora = obj.auex_hora.split(":");
            hora[0] = hora[0] != '10' ? hora[0].replace("0", "") : hora[0];
            var processo = obj.auex_proc_id == 1 ? "smt" : "";

            /*Eu dei o console.log para ver oq estava sendo executado, se eu pegar o que printou no console, e executar por lá mesmo, funciona*/
            console.log("$(\".div-pai[data-proc-id='"+obj.auex_proc_id+"']\").find(\"#tab"+hora[0]+processo+"\").find(\".check-ignorar-horario\").attr('checked', true);");
            console.log("$(\".div-pai[data-proc-id='"+obj.auex_proc_id+"']\").find(\"#tab"+hora[0]+processo+"\").find(\".chck\").attr('disabled', 'disabled');");

            /*Estas são as duas instruções que não funcionam*/
            $(".div-pai[data-proc-id='"+obj.auex_proc_id+"']").find("#tab"+hora[0]+processo).find(".check-ignorar-horario").attr('checked', true);
            $(".div-pai[data-proc-id='"+obj.auex_proc_id+"']").find("#tab"+hora[0]+processo).find(".chck").attr('disabled', 'disabled');
        });
    }
});

Can anyone see any mistake I’m not seeing?

  • 1

    Maybe the elements don’t exist at the moment they try to be used?

  • the worst they exist yes, that my ajax is a function q I only run when everything is already loaded

  • @Lucascosta, maybe that’s right, because I gave a setTimeout of 800mls and it worked... But there arises another doubt, this ajax is within a function, which is only executed in . done from another ajax... he would not have to wait to complete the first to accomplish the second?

2 answers

2

Probably the call of your instructions is being made before the elements are loaded.

Try to put the script to be loaded after its elements.

Or put your function inside that call:

     $(function(){
       //seu código aqui
    });

0

Try it this way

$(function(){ 
$.ajax({
    url: addressWebService+'auditoriaexcecao/getAuditorias/audi_id/'+audi_id,
    type: 'get',
    dataType: 'json'
}).done(function(r){
    if(r.length >= 1){
        var hora;
        $.each(r, function(i, obj){

            hora = obj.auex_hora.split(":");
            hora[0] = hora[0] != '10' ? hora[0].replace("0", "") : hora[0];
            var processo = obj.auex_proc_id == 1 ? "smt" : "";

            /*Eu dei o console.log para ver oq estava sendo executado, se eu pegar o que printou no console, e executar por lá mesmo, funciona*/
            console.log("$(\".div-pai[data-proc-id='"+obj.auex_proc_id+"']\").find(\"#tab"+hora[0]+processo+"\").find(\".check-ignorar-horario\").attr('checked', true);");
            console.log("$(\".div-pai[data-proc-id='"+obj.auex_proc_id+"']\").find(\"#tab"+hora[0]+processo+"\").find(\".chck\").attr('disabled', 'disabled');");

            /*Estas são as duas instruções que não funcionam*/
            $(".div-pai[data-proc-id='"+obj.auex_proc_id+"']").find("#tab"+hora[0]+processo).find(".check-ignorar-horario").attr('checked', true);
            $(".div-pai[data-proc-id='"+obj.auex_proc_id+"']").find("#tab"+hora[0]+processo).find(".chck").attr('disabled', 'disabled');
        });
    }
}); 
});

Browser other questions tagged

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