Get id from a dynamic select with jquery

3

I need to get the neighborhood3 id, I'm doing this

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

But it still does not work.

You have to add a variable in #boirro, for each form that opens have a different id # neighborhood1 # neighborhood2 # neighborhood3 ..., how can I do that, I imagine I would have to do this for id #rua also # rua1 # street2 # street3 ...

$(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>');

        }
    });

});

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>
    
asked by anonymous 09.05.2018 / 11:44

1 answer

0

So the code stayed, it worked

$(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', '[name^="bairro"]', function(){ var bairroID = $(this).val();
        console.log("ok");

        if(bairroID){
            $.ajax({
                type:'POST',
                url:'ajax_data2.php',
                data:'bairro_id='+bairroID,
                success:function(html){
                    $('#rua[name^="rua"]').html(html);

                }
            }); 
        }else{
            $('#rua').html('<option value="">Select categoria first</option>');

        }
    });

});
    
09.05.2018 / 13:25