Fill Select by creating by append Javascript

Asked

Viewed 81 times

0

Opa,

I have a button to create some inputs, I need to fill a select when creating it with data from the bd, I did so, I played in a variable javascript 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 the completion of select fez:

            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";
                });
            }

Works only after creating the first select, after which the selects are created, but are not filled.

To create the inputs I am calling the append with this code:

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

        preenche_select();

    });
  • already debugged to see if the data is really coming to the code that generates the "options"?

  • already gave an Alert inside the fill_list: Alert(opts ); function. It displays messages normally, but does not fill select

  • on the console printed 'opts.split is not a Function'

  • are you sure opts is a string? this here simulates this console error: "var a = []; a.split(';');", make sure that opts is no longer an array.

No answers

Browser other questions tagged

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