循环对象并附加到容器

时间:2011-10-06 10:29:41

标签: javascript jquery

在jQuery中,我如何获取DATA中的每个'itemName'并将其附加到posts-container?

所以我会有像这样的HTML .....

标题:哇刚见过这个

标题:测试

等...

由于

的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;
});

1 个答案:

答案 0 :(得分:1)

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

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