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.– Sam
there is some way I can call Trigger before the function?
– Carlos Rocha
At the base of the gambiarra has:
$( function () {
 setTimeout(function(){
 $( "#idSetor" ).trigger( "change" );
 }, 100);
});
.... Trigger will be fired 1 tenth of a second after page loading.– Sam
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()?
– Carlos Rocha
$("#idSetor").prop("tagName")
... the value will be INPUT or SELECT (even high)– Sam
if ( $( "#idSetor" ).attr('type') == "Hidden" ) $("#idSetor"). Trigger("change");
– Carlos Rocha
.prop("tagname") is better?
– Carlos Rocha
Let’s go continue this discussion in chat.
– Sam