使用JSON响应创建下拉菜单

时间:2015-03-13 13:36:23

标签: jquery

好的我正在尝试用JSON响应来填充组合框,就像这样......

  [{"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}]

数字应该是值,Text是文本,而对于第3个参数,我有false / true选项。如果为true,则应在下拉菜单中选择attr。

我在想创建标签  标签

并使用j查询添加选项,带有一些循环。也许有人知道如何做到这一点

2 个答案:

答案 0 :(得分: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);
}

小提琴示例:http://jsfiddle.net/xhh4wqv7/

答案 1 :(得分:0)

&#13;
&#13;
  var tiposItems = "<option value='" + valueSelectOne + "'>" + textSelectOne + "

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