循环遍历对象并追加到容器

loop over an object and append to a container

本文关键字:追加 遍历 对象 循环      更新时间:2023-09-26

在jQuery中,我如何获得'itemName'为每个在DATA和附加到post -container?

所以我要有这样的html .....

title:刚刚看到这个

标题:测试

等等……

感谢Json

{
    "ERRORS": [],
    "DATA": [
        {
            "itemID": "012be513-9b2d-4ed6-9b84-4097a610a823",
            "itemValidTo": "October, 29 2011 00:00:00",
            "itemName": "wow just seen this",
            "image": {
                "imageCreated": "October, 06 2011 00:00:00",
                "imageURL": "ha.jpg",
                "imageID": "16fbdb40-8591-4a3b-b18e-bda494ac213f"
            },
            "itemActive": true,
            "createdBy": {
                "UserID": "DA1F154B-EF46-49BD-18C84C49478AD48E",
                "accNumber": ""
            },
            "itemTxt": "SO impressed with this!",
            "itemModified": "October, 06 2011 00:00:00"
        },
        {
            "itemID": "30d2f2c1-58ca-4b3d-b3e0-d284ae5b25ab",
            "itemName": "test",
            "image": {
                "imageCreated": "October, 05 2011 00:00:00",
                "imageURL": "image.jpg",
                "imageID": "bc869a94-fee5-4fc8-bd21-e2de2f020310"
            },
            "itemActive": true,
            "createdBy": {
                "UserID": "DA1F154B-EF46-49BD-18C84C49478AD48E"
            },
            "itemTxt": "test",
            "itemModified": "October, 05 2011 00:00:00"
        }
    ],
    "MESSAGES": [
        {
            "TOTAL": 2,
            "CURRENTPAGE": 1,
            "TOTALPAGES": 1
        }
    ]
}
脚本

<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        type: 'get',
        url: 'http://do.com/api/id/123',
        dataType: 'json',
        data: '',
        success: function(r){
            $('#waiting').hide(500);
            var output = '';
            //loop here and add append I assume?

            jQuery('#posts-container').append(output);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
        }
    });
    return false;
});

$.each(r.DATA,function(key,value){
    alert(value.itemName)
    alert(value.itemID)
    .
    .
})

你可以从这里查看http://jsfiddle.net/Uk5ZJ/