简体   繁体   English

用JSON数据填充下拉列表

[英]Populate dropdown with json data

I am trying to populate a dropdownbox with data from a JSON page. 我正在尝试使用JSON页面中的数据填充下拉列表。

Here is the code I am using: 这是我正在使用的代码:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $.ajax({
            url: "json/wcf.svc/GetTax",
            dataType: 'json',
            data: data
        });

        $($.parseJSON(data.msg)).map(function () {
            return $('<option>').val(this.value).text(this.label);
        }).appendTo('#taxList');
    });
</script>

And here is what the json data returns: 这是json数据返回的内容:

{"d":"{\"16\":\"hello\",\"17\":\"world\"}"}

I am getting an error that "data is undefined." 我收到“数据未定义”的错误消息。 Do I have to somehow tell JQ how to read the json data? 我是否必须以某种方式告诉JQ如何读取json数据?

Firstly, the data variable you are passing to the ajax call is not defined (well, not in the code sample you provided), and secondly the ajax call is happening asynchornously, so you need to do something with the returned data, ie via a success callback. 首先,没有定义要传递给ajax调用的数据变量(好吧,不是在您提供的代码示例中),其次,ajax调用是异步发生的,因此您需要对返回的数据执行某些操作,即通过a成功回调。 Example: 例:

$(document).ready(function () {
    var data = //define here
    $.ajax({
        url: "json/wcf.svc/GetTax",
        dataType: 'json',
        data: data, // pass it in here
        success: function(data)
        {
            $(data.msg).map(function () {
                return $('<option>').val(this.value).text(this.label);
            }).appendTo('#taxList');
        }
    });       
});

also you shouldn't need to parse the data returned from the ajax call, as jQuery will automatically parse the JSON for you, ( should need the $.parseJSON(data.msg)) 同样,您也不需要解析ajax调用返回的数据,因为jQuery会自动为您解析JSON((需要$ .parseJSON(data.msg)))

EDIT 编辑

Based on the interesting format of the JSON, and assuming that it cannot be changed, this should work (ugly though) 基于JSON的有趣格式,并假设它无法更改,这应该可以工作(虽然很丑)

$(document).ready(function () {
        var data = //define here
        $.ajax({
            url: "json/wcf.svc/GetTax",
            dataType: 'json',
            data: data, // pass it in here
            success: function(data)
            {
                 data = data.d.replace(/{/g, '').replace(/}/g, '').split(',');
                 var obj = [];
                 for (var i = 0; i < data.length; i++) {
                     obj[i] = {
                      value: data[i].split(':')[0].replace(/"/g, '').replace('\\', ''),
                      label: data[i].split(':')[1].replace(/"/g, '')
                     };
                 }
                 var htmlToAppend = "";
                 for (var j = 0; j < obj.length; j++) {
                     htmlToAppend += '<option value="' +
                         obj[j].value +
                         '">' + obj[j].label +
                         '</option>';
                 }
                 $('#taxList').append(htmlToAppend);
            }
        });       
    });

You need to use the success option to return the data. 您需要使用success选项来返回数据。

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $.ajax({
            url: "json/wcf.svc/GetTax",
            dataType: 'json',
            success: function(data){

           $(data.msg).map(function () {
            return $('<option>').val(this.value).text(this.label);
        }).appendTo('#taxList');

        }
        });


    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM