Fill in Select by creating by append Javascript

0

Opa,

I have a button to create some inputs, I need to fill a select when creating it with data from bd, I did so, I played in a javascript variable the values of the sql query.

    foreach ($operadoras as $key => $value) {
      $val_operadoras = $val_operadoras.$value['id'].':'.$value['descricao'].';';
    }

    echo '
      <Script type="text/javascript">
        var opts = "'.$val_operadoras.'";
      </script>
    ';

For filling the select I did:

            function preenche_select()
            {

                opts = opts.split(';');
                var target = $(".id_operadora");
                $.each(opts, function(i, opt){
                  opt = opt.split(':');
                  target.append($('<option />').val(opt[0].trim()).text(opt[1].trim()));

                  document.getElementById("id_operadora").selectedIndex = "4";
                });
            }

It works only after creating the first select, after that the selects are created, but are not filled in.

To create the inputs I'm calling append with this code:

    $('#add').live('click', function() { 
        addCampo();

        preenche_select();

    });
    
asked by anonymous 14.04.2016 / 20:50

0 answers