Ajax does not work when it comes from a form select

Asked

Viewed 86 times

2

When the #sectionChooser comes from Ajax or does not work select #neighborhoods to put the #street, where would be the problem?

Html

 <div class="col-sm-6">
                            <label for="Planos">Planos</label>

              <select name="Planos" id="sectionChooser" class="form-control valid" aria-invalid="false">              

              <option value="">Selecione</option>                   
    <option value="1">Diamante</option><option value="2">Ouro</option><option value="3">Prata</option><option value="4">Light</option><option value="5">Free</option><option value="19">Link</option>                   
              </select>         

    <div class="panel" id="1" style="display: block;">    

    <div class="col-sm-12">
                                    <label><b>Diamante</b></label>
                                    </div>
                                    </div>
<div class="col-sm-6">
                        <label for="Bairro">Bairro</label>
                        <select class="form-control valid" name="bairro" id="bairro3" required="" aria-invalid="false">
    <option value="">Selecione</option>                 
<option value="1">teste bairro</option><option value="2">teste bairro 2 3</option><option value="3">ação bairros</option><option value="4">Centro</option>


    </select> 
                    </div>

<div class="col-sm-6">
                        <label for="Rua">Rua</label>



<select class="form-control" name="rua" id="rua" required="">





    </select> 




                    </div>

I’m trying like this, but it still didn’t work

$(document).ready(function(){

    $(document).on('change', '#sectionChooser', function(){
        var myID = $(this).val();
        $('.panel').each(function(){
            myID === $(this).attr('id') ? $(this).show() : $(this).hide();
            console.log("ok");
        });
    })


    $(document).on('change', '#bairro', function(){
        var bairroID = $(this).val();

        if(bairroID){
            $.ajax({
                type:'POST',
                url:'ajax_data2.php',
                data:'bairro_id='+bairroID,
                success:function(html){
                    $('#rua').html(html);
                    console.log("ok");
                }
            }); 
        }else{
            $('#rua').html('<option value="">Select categoria first</option>');

        }
    });

});
  • This #sectionChooser comes from Ajax?

  • yes it comes from ajax, it hasn’t worked yet

  • Pass your html too so we can simulate your problem.

  • I put in the reply

  • Do not change the question according to the answers. Otherwise we lose as it was.

  • Where has elemento is just one example... in place of elemento you put the selector... see the answer.

  • you don’t need to change anything in your code, just the two events I put in the answer.

Show 2 more comments

2 answers

0

The problem is that it is selecting the id wrong: instead of #bairro, the correct would be #bairro3, see:

                                                  id correto
                                                       ↓
<select class="form-control valid" name="bairro" id="bairro3" required="" aria-invalid="false">

When using Ajax, instead of using events .change, use .on:

$(document).on('change', '#sectionChooser', function(){

and

$(document).on('change', '#bairro', function(){

Then you will have no delegation problems with elements dynamically inserted on the page.

The code should look like this:

$(document).ready(function(){

    $(document).on('change', '#sectionChooser', function(){
        var myID = $(this).val();
        $('.panel').each(function(){
            myID === $(this).attr('id') ? $(this).show() : $(this).hide();
        });
    })


    $(document).on('change', '#bairro3', function(){
        var bairroID = $(this).val();

        if(bairroID){
            $.ajax({
                type:'POST',
                url:'ajax_data2.php',
                data:'bairro_id='+bairroID,
                success:function(html){
                    $('#rua').html(html);
                }
            }); 
        }else{
            $('#rua').html('<option value="">Select categoria first</option>');

        }
    });

});
  • hasn’t worked yet friend, strange that does not return any error on the console

  • put a console.log("ok"); inside events to see if you’re at least getting the click.

  • here yes $(Document). on('change', '#sectionChooser', Function(){ more here $(Document). on('change', '#neighborhood', Function(){ not

  • look at my question I edited with the console.log("ok");

  • Place right after the opening of the event to see if the click is heard, and not in the middle.

  • I did $(Document). on('change', '#neighborhood', Function(){ console.log("ok"); and did not return anything yet, only in $(Document). on('change', '#sectionChooser', Function()ķ returns "ok"

  • I arranged the html in the question and put the neighborhoods and street

  • 1

    The problem is that the id is #bairro3 and not #bairro

  • Hmm understood, I’ll see what I do, thank you

  • But he should get the id Bairro3 here $(Document). on('change', '#neighborhood', Function(){ var bairroID = $(this). val(); but it still doesn’t work

Show 6 more comments

-1

To catch the <option> selected:

$('#sectionChooser option:selected').val();
  • hasn’t worked out yet, buddy

  • So the error is not there, because then you take the selected option, you can give an Alert for you to see the value.

  • As I did in the reply, I put html too

Browser other questions tagged

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