-3
var result={"clientIds":[{"id":0,"clientId":"check123"},{"id":1,"clientId":"check234"}]};
$.each(result, function (key, data) {
    $.each(data, function (index1, valindex) {
        $.each(valindex,function(finalk,finalv) {
            alert("Final Key::::"+finalk+"Final Value::::"+finalv);
            $('#MySelect').append($("<option value='finalv'></option>").val(finalv.Value).text(finalv.Text));
            //  alert("Working"); 
        });
    });
 });

I have code like this. In last each loop I get values 0,check123,1,check234

The above values I want to put in a dropdown

My code is:

$('#MySelect').append($("<option></option>").val(finalv.Value).text(finalv.Text))

When I execute the code, field's name is not displayed in dropdown. I want value names as each option. How can I do this?

Daniel Schneller
  • 13,728
  • 5
  • 43
  • 72

1 Answers1

0

If you need to fill a dropdown with JSON data,you need to parse it first.Here is a stackoverflow link that shows the same.

You need to separate the Key-Value pair from the JSON object, put it in an option tag and append the option tag to dropdown.

I suggest you to try this,

$.each(result, function (key, data) {
    $.each(data, function (index1, valindex) {
        $($.parseJSON(valindex)).map(function () {
            return $('<option>').val(this.value).text(this.label);
        }).appendTo('#MySelect');
        //  alert("Working"); 
    });
});
Community
  • 1
  • 1
Adersh M
  • 596
  • 3
  • 19