Load combobox with $.getJSON data

Asked

Viewed 1,096 times

1

I’m doing a method that goes in the comic book makes a query and returns the codes.

With these codes I charge to Combobox with the respective items.

There are times that fill in neat, there are times that don’t fill out... Some idea?

$.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 thing is when I put one alert before completing the Combo, it works :)

  • 1

    Put your HTML and what’s in that JSON or it will be hard to help. Trying to guess, maybe this will help you? http://jsfiddle.net/0pdxwhx0/

  • Instead of putting an Alert, use the console.log(...). This way it is easier to understand what happened because the synchronization problem will continue to occur. In addition, you could use presets and print the results in the events of done, fail and always.

  • Hello Eduardo! The whole function is this ai...it returns 5 fields of php... What I realize is that he brings the data right, but give these bugs when filling the . val

  • Dude, I don’t quite understand what you said...but you said it right. It’s some timing problem...

  • @Rafaelspessotto what gives console.log(JSON.stringify(data)); if you put it before the $.each?

  • Sergio returns normal BD data...but does not fill the combo with this information... :(

  • @Rafaelspessotto you can show here what gives the console.log(JSON.stringify(data));? I have no idea what your JSON is like, and seeing it makes it easier.

Show 2 more comments

1 answer

1

Fala Rafael,

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

To popular a combo box through 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>

<select id="comboFrutas"></select>

  • Speaks Gustavo! Thanks for the reply.... But the combo is filled previously yes.. I have a routine that fills it... The problem is in precisely I can fill the combo, with the ID of the item that came from the BD.. Example: ID 2 ITEM: RAFAEL... I go in the BD, I take the value 2, and I want the combo to have the value "RAFAEL", then I use the .val... only that when I run in the browser, it gives some problem, because it has hours that it changes the value to "RAFAEL" and hours that it does nothing and stays in the first item added..

  • Got, in order to help you, we need to know what is returning in the variable data; ?

  • {"cord":[{"ID":"8","ID_USUARIO":"2","ID_CORD":"1","ESP1":"63","ESP2":"104","ESP3":"105","ESP4":"106","ESP5":"0","ESP6":"0"}]}

  • This is the return Gustavo... Thanks for your help!

Browser other questions tagged

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