dongsuiying7773 2017-03-17 06:44
浏览 94
已采纳

使用jQuery和PHP在div中显示循环中的JSON数据

I am using jQuery and JSON to get data from database

$("#openNotification").on('click', function(e)
{
    ID = "a";
    $.ajax(
    {
        url: "notificationDetail",
        type: "POST",
        data:
        {
            designationID: ID
        },
        dataType: "JSON",
        success: function (jsonStr)
        {
            $(".processingNotification").hide();

            var jSonLength = jsonStr.length;

            for (var i=0; i<jSonLength; i++)
            {
                var userAvatar = jsonStr[0].userAvatar;

                var newrow = "<div class='subNotification'><img src='assets/img/avatar/"+userAvatar+"'/></div>";


            }


            $(".opened_page").html(newrow);
        }
    });
});

HTML

<div class="opened_page"></div>

When I click #openNotification I can see the jSon data from PHP

enter image description here

But when I want to display the data to .opened_page it only show 1 data.

  • 写回答

2条回答 默认 最新

  • dongzuo4666 2017-03-17 06:47
    关注

    you are replacing with new value, code should be

    var newrow = '';
    for (var i=0; i<jSonLength; i++)
    {
         var userAvatar = jsonStr[0].userAvatar;
         newrow += "<div class='subNotification'><img src='assets/img/avatar/"+userAvatar+"'/></div>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料