首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Ajax调用返回需要追加到选择框中的Json

Ajax调用返回需要追加到选择框中的Json
EN

Stack Overflow用户
提问于 2018-07-25 03:57:46
回答 1查看 13关注 0票数 0

几个小时以来,我一直在为将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>');


                    });
            }
        });
    });
EN

回答 1

Stack Overflow用户

发布于 2018-07-25 04:14:00

我自己就能解决这个问题,我想通了!除了使用

 $.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>');

                        });
                }
            });
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51506614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档