附加json_encode的问题

时间:2011-09-27 14:29:08

标签: javascript jquery append each

我想通过service附加jQuery.each(),但是我的js代码中没有用吗?

这是我的PHP代码输出:

{
    "data": [{
        "service": ["shalo", "jikh", "gjhd", "saed", "saff", "fcds"],
        "address": "chara bia paeen"
    }, {
        "service": ["koko", "sili", "solo", "lilo"],
        "address": "haminja kilo nab"
    }, {
        "service": ["tv", "wan", "hamam", "kolas"],
        "address": "ok"
    }]
}

这是我的jQuery代码:

$.ajax({
    type: "POST",
    dataType: "json",
    url: 'get_residence',
    data: dataString_h,
    cache: false,
    success: function (respond) {
        $.each(respond.data, function (index, value) {
            $('ol li').append('<a href="">' + value.service + '</a>');
        });
    },
    "error": function (x, y, z) {
        alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
    }
});

我该怎么办?

1 个答案:

答案 0 :(得分:2)

你需要另一个$ .each循环,因为service是一个数组:

 $.each(respond.data, function (index, value) {
     $.each(value.service, function () {
         $('ol li').append('<a href="">' + this + '</a>');
     });
 });

只是格式化数组:

 $.each(respond.data, function () {
     $('ol li').append('<a href="">' + this.service.join(', ') + '</a>');
 });