将数组对象列表绑定到组合框

时间:2014-03-24 09:50:12

标签: jquery asp.net combobox

我有一个在某个对象里面的数组。如何将对象绑定到组合框列表。

组合框代码:

 <select name="select-native-5" id="cmbDty"></select>

我需要将Id和Ad字段绑定到组合框

和我的数组对象; enter image description here

这是我如何得到对象数组

Jquery代码:

 $("#select-native-11").change(function () {
               var dd = $("#select-native-11").val();
                $.ajax({
                type: "POST",
                url: "MasaSiparis.aspx/AltMenuGetir2",
                data: "{'p':'" + dd + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert(data.d);
                        $.each(data.d, function (val, text) {
                        alert(val);
                       //I need to put here 
                        });
                });
                    },
                error: function () { alert('HATA');}
            });
        });

2 个答案:

答案 0 :(得分:1)

您需要获取下拉列表的参考,并在下拉列表中添加选项..

 $("#select-native-11").change(function () {
                   var dd = $("#select-native-11").val();
                    $.ajax({
                    type: "POST",
                    url: "MasaSiparis.aspx/AltMenuGetir2",
                    data: "{'p':'" + dd + "'}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        alert(data.d);
var x = document.getElementById("mySelect");
                            $.each(data.d, function (val, text) {
                            alert(val);
                           //I need to put here 
                          //here is your dropdownid replace with "select"

                          var option = document.createElement("option");
                          option.text = text;
                          option.value = val;
                          x.add(option);

                            });
                    });
                        },
                    error: function () { alert('HATA');}
                });
            });

答案 1 :(得分:0)

这是解决方案;

$.ajax({
                type: "POST",
                url: "MasaSiparis.aspx/AltMenuGetir2",
                data: "{'p':'" + dd + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                    var x = document.getElementById("select-native-5");
                    for (var i = 0; i < data.d.length; i++) {
                        //alert(data.d[i]); //"aa", "bb"

                        var option = document.createElement("option");
                        for (var key in data.d[i]) {
                           alert(key + ': ' + data.d[i][key]);

                            if (key == "AltMenuId") {
                                option.value = data.d[i][key];
                            }

                            if (key == "Ad") {
                                option.text = data.d[i][key];
                            }

                        }
                        x.add(option);
                    }

                },
                error: function () { alert('HATA'); }
            });

*关键点正确循环