0

Ok i am trying to populate combobox with JSON response, something like this...

  [{"uid":"95bf721c-8c24-432e-83e4-7233169f23fa","name":"MARKETING I MARKETING MENAD\u017dMENT","selected":false},{"uid":"ed4293ff-9c89-4052-9fec-852d34c6150d","name":"PRODAJA I MENAD\u017dMENT PRODAJE","selected":false}]

Numbers should be values, Text is a text, and for 3th parametar i have false/true option. If is true value should have attr selected in dropdown menu.

I am thinking creating tag tag

and with j query add options, with some loop. Maybe someone have idea how to do that

2 Answers 2

1
var arr = [{"uid":"95bf721c-8c24-432e-83e4-7233169f23fa","name":"MARKETING I MARKETING MENAD\u017dMENT","selected":false},{"uid":"ed4293ff-9c89-4052-9fec-852d34c6150d","name":"PRODAJA I MENAD\u017dMENT PRODAJE","selected":true}];

var sel = $('<select/>');
for (var i = 0;i<arr.length;i++) {
    var obj = arr[i];
    var opt = $('<option/>');
    opt.val(obj.uid).text(obj.name)[0].selected = obj.selected;
    sel.append(opt);
}

Fiddle example: http://jsfiddle.net/xhh4wqv7/

Sign up to request clarification or add additional context in comments.

7 Comments

What do you mean if all is false?
in some case i will not have selected value in dropdown menu, in JSON response i will have 3th parametar false for all values
Oh that's fine then. If you change it to false in the fiddle you will see it defaults to the first one in the select just as a standard select element would with nothing selected.
my mistacke tnx mate!
Anytime. ;) Happy to help.
|
0

  var tiposItems = "<option value='" + valueSelectOne + "'>" + textSelectOne + "

      $.each(JSONdata, function (i, tipo) {
        tiposItems += "<option value='" + tipo.Value + "'>" + tipo.Text + "</option>";
      });
      $('#yourDDL').html(tiposItems);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.