Load combobox with $ .getJSON data

1

I'm doing a method that goes in the DB does a query and returns the codes.

With these codes I load Combobox with their respective items.

There are times when it fits right in, there are times when it does not fill ... Any ideas?

$.getJSON("carrega.php?id_usuario=" + id_usuario + "&id_cord=" + id_cordao,function(data){
    $.each(data.cord, function(i,user){         
        //Carrega o Combo com o Valor do BD
        $('#cbo1').val(user.cord1);
        $('#cbo2').val(user.cord2);
        $('#cbo3').val(user.cord3);
        $('#cbo4').val(user.cord4);                 
    }); 
});

Funny that when I put a alert before filling the Combo , it works:)

    
asked by anonymous 20.05.2015 / 23:04

1 answer

1

Speak Rafael,

When you use .val(valor) in a combo box, you are actually selecting an existing item in the box .

To populate a combo box with database results, you can do this:

var frutas = ['maca','pera','uva'];

var _htmlOptions = "";
$.each(frutas,function(i,fruta){
   _htmlOptions += "<option val='"+fruta+"'>"+fruta+"</option>";
});

$("#comboFrutas").append(_htmlOptions);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><selectid="comboFrutas"></select>
    
23.05.2015 / 23:10