Bootstrap selectpicker class, does not recognize insertion of <option> via jquery (.html())

Asked

Viewed 814 times

0

I’m using the Bootstrap framework, and the Selectpicker class to receive city data by status. The script works well, returns data, but instead of inserting <option> Cidade X </option> he just inserts Cidade X. I know the problem is in class, but I don’t know how to fix it. Class:

<div class="col-md-3  col-xs-4" id="cidade">

<select data-width="auto" id="municipio" name="municipio" class="selectpicker municipio">
    <option value="0" disabled="disabled">Selecione o estado</option>
</select>
</div>

Script:

    $(document).ready(function(){

    $("#estado").change(function() {

        var est = document.getElementById("estado").value; 

        debugger;

        $.ajax({
        type: 'post',
         url: 'cidade.php',
         data: {
          cd_estado: est
         },
         success: function(dados) {
            $(".municipio").html(dados);
         }
         });

    });
});
  • I was able to solve my problem which was the same as the link: https://stackoverflow.com/questions/17941749/bootstrap-select-notworking

1 answer

0

When you use the . html () you override including the select element, imagining that you are bringing with ajax the "options" of this select, use the append() function to add child elements in the select.

Edit: bring the mounted element from your controller, that is, bring it within the tags "option".

  • I’m bringing in the assembled data <option value="Cod_Cidade"> Cidade X </option>. I used your suggestion, but with the append() is also overwriting the select. That class selectpicker brings many features, but it is difficult to change your content via javascript. I’m not getting it yet. Thanks for the tip.

  • Well, I saw that Voce depends on another combobox, so to solve your problem bring the select of the cities complete the element and options. Create a Div to receive the component.

  • Didn’t resolve. must be bootstrap class protection. I could only resolve by not using . selectpicker :(

Browser other questions tagged

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