需要附加到选择框的Ajax调用重调Json

时间:2018-07-24 19:57:46

标签: json ajax select dynamic each

我一直在努力将Json数据放入选择框中已有几个小时,我能够成功在Ajax内检索json数据。

问题是ID出现在选择框中,作为带有名称的选项。我不希望我只希望显示名称,而ID值保持隐藏。

我到目前为止所拥有的代码。

 <select id="seasontype_select">
            <option value=""> select</option>
  </select>

Json数据:

[{"value":"1","label":"BrushPickup"},{"value":"2","label":"LeafPickup"}]

Ajax:

             $.ajax({
            url: 'SelectType.ashx',
            method: 'POST',
            dataType: 'json', //make sure your service is actually returning json here
            contentType: 'application/json',
            data: '{}',
            success: function (data, status) {


  //Here Is Where I get the Value: ID, with Label don't want that just Label should show 
                     $.each(data, function (i, d) {
                        $('#seasontype_select').append('<option value"' + i + '">' + d + '</option>');


                    });
            }
        });
    });

1 个答案:

答案 0 :(得分:0)

我能够自己解决这个问题,我发现了!除了使用

 $.ajax({
                url: 'SelectType.ashx',
                method: 'POST',
                dataType: 'json', //make sure your service is actually returning json here
                contentType: 'application/json',
                data: '{}',
                success: function (data, status) {
                        $.each(data, function (i,d) {
                            $('#seasontype_select').append('<option value"' + d.value + '">' + d.label + '</option>');

                        });
                }
            });
        });