Fire jQuery using the Hidden input

Asked

Viewed 141 times

1

FORM

echo "<input type='hidden' name='idRede'   value=" . $idRede . " /> ";
echo '  
        <label class="labelPequeno" for="idRegiao">Região</label> :
        <select name="idRegiao" id="idRegiao" class="inputTextMedio required">
            <option value="" selected>Escolha a Rede primeiro</option>
        </select> <br/>

I have the jQuery to popular the Region:

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

        $.ajax({
            url: "_scripts/_php/_buscas/buscarDadosRegioes.php",
            type: "POST",
            dataType: "json",
            data: {
                  idRede: $("#idRede").val()
            },
            beforeSend: function() {
                $("#imgCarregando").css('display','block');
            },
            success: function (result) {
                $("#imgCarregando").css('display','none');
                $('#idRegiao').find('option').remove();

                if (result == null){
                    $("#idRegiao").append("<option value=>Sem Regiões</option>");
                } else {
                    $("#idRegiao").append("<option value=>Escolha a Região</option>");                      
                    result.forEach(function(option){
                        $("#idRegiao").append("<option value=" + option["idRegiao"] + ">" + option["nome"] + "</option>")
                    });
                }
            }

        });

    });

The problem is that before, idRede was a SELECT but now it’s a input hidden.

Some way to fire jQuery using the input hidden?

Grateful to those who can help!

  • I’ve had such a problem with the Hidden fields, already tried using the bind? $("#idRede").bind("change", function () {

  • and how I pass the value in Bins?

1 answer

1


Just put the id #idRede in the Hidden input and trigger the event change manually using the method .trigger() jQuery.

Field:

echo "<input type='hidden' name='idRede' id="idRede" value=" . $idRede . " /> ";
                                         ↑↑↑↑↑↑↑↑↑↑↑

jQuery:

$(function(){

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

});
  • Boys, tell you it worked!

  • Mt good! thanks! D

  • tin tried just like echo '<script>$("#idArea"). Trigger("change");</script>'; and it didn’t work. You really need to call $(Function(){?

  • The $(function(){ expects the DOM to be loaded if you are calling before the element.

  • because that’s why I’m confused, because I put after the element: echo '<script>$("#idRede"). Trigger("change");</script>';

  • But it has to be after the $("#idRede").on("change", function () {

  • this call $("#idRede"). on("change", Function () { gets in the page header, ie bemmmmm there at the beginning of the page and the ("#idRede").. change(); down there

Show 2 more comments

Browser other questions tagged

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