on Change does not starta after Trigger();

Asked

Viewed 29 times

0

I have the following code:

<script type="text/javascript" src="_scripts/_js/jquery-2.1.4.min.js"></script>

<input type='hidden' id='idSetor' name='idSetor' value=1/>

<script>
    $( function () {

        $( "#idSetor" ).trigger( "change" );

    } );
</script>

<script>
    $( document ).ready( function ( e ) {

        $( "#idSetor" ).on( "change", function () {

            if ( $( "#idSetor" ).val() != "" ) {

                $.ajax( {
                    url: "_scripts/_php/_contagens/contaCelulas.php",
                    type: "POST",
                    dataType: "json",
                    data: {
                        idCelula: $( "#idSetor" ).val()
                    },
                    beforeSend: function () {
                        $( "#imgCarregando" ).css( 'display', 'block' );
                    },
                    success: function ( result ) {

                        $( "#imgCarregando" ).css( 'display', 'none' );

                        if ( result >= 4 ) {
                            $( "#conta" ).html( "<h1>Esse Setor já possui " + result + " Células!</h1>" );
                            $( "#conta" ).css( "display", "block" );
                            $( "#escolhaCelula" ).css( "display", "none" );
                        } else {
                            $( "#conta" ).css( "display", "none" );
                            $( "#escolhaCelula" ).css( "display", "block" );
                        }

                    }

                } );

            } else {
                $( "#conta" ).html( "<h1>Escolha a Rede!</h1>" );
                $( "#conta" ).css( "display", "block" );
                $( "#escolhaCelula" ).css( "display", "none" );
            }


        } );

    } );
</script>

<div id="conta"></div>

<div id="escolhaCelula" style="display: none;">
    <label class="labelPequeno" for="nome">Nome da Célula</label> : <input type="text" class="inputTextMedio required" name="nome" id="nome" required/><br/><br/>
    <button id="buttonCadastrarCelula" class="button">Cadastrar</button>
    <img id="imgCarregando" style="display: none" src="_imgs/carregando.gif"/><br/>
</div>

<div class="resposta"></div>

I wonder why, after the

$( "#idSetor" ).trigger( "change" );

the function

$( "#idSetor" ).on( "change", function () {}

Does not rotate?

I mean, don’t get into onchange

  • The $( "#idSetor" ).on( "change", function () {} is coming after Trigger. So Trigger has not yet met the event that is trying to shoot.

  • there is some way I can call Trigger before the function?

  • At the base of the gambiarra has: $( function () {&#xA; setTimeout(function(){&#xA; $( "#idSetor" ).trigger( "change" );&#xA; }, 100);&#xA;});.... Trigger will be fired 1 tenth of a second after page loading.

  • I have a form validation that, depending on the situation, the idSetor field, needs to be chosen in a select and depending on it is loaded along with the page in an Hidden input. Is there a way to check where the idSetor value is coming from? Whether it comes from a Select or an Input Hidden before calling Trigger()?

  • $("#idSetor").prop("tagName")... the value will be INPUT or SELECT (even high)

  • if ( $( "#idSetor" ).attr('type') == "Hidden" ) $("#idSetor"). Trigger("change");

  • .prop("tagname") is better?

Show 3 more comments
No answers

Browser other questions tagged

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