doue2666 2017-02-20 07:30
浏览 33
已采纳

每次在codeigniter中完成ajax成功函数循环时,尝试将数据添加到新div

Hey guys i am trying to send the data from the loop to the div element so that i can show it one by one . but when i try to send the data it just shows all the values together what can i do ?

here is my code

<script type="text/javascript">
    $(document).ready(function(){
        $(window).scroll(function(){
            var lastID = $('.load-more').attr('lastID');
            if ($(window).scrollTop() == $(document).height() - $(window).height() && lastID != 0){
                jQuery.ajax({
                    type:'POST',
                    url:'<?php echo base_url("user/get_all_post"); ?>',
                    data: "id=" + lastID,
                    dataType: 'json', 
                    beforeSend:function(html){
                        $('.load-more').show();
                    },
                    success:function(data){
                        var ParsedObject = JSON.stringify(data);            
                        var json = $.parseJSON(ParsedObject);
                        $PostId=json[4]['id'];
                        for(i=0;i<5;i++ )
                        {
                            var post_status = json[i]['status'];
                            var status_image = json[i]['status_image'];
                            var multimage = json[i]['multimage'];
                            alert(post_status);
                            $("#post_status").show();
                            $("#status_data").append(post_status);
                            $("#post_status").hide();
                        }
                        $('.load-more').attr('lastID', $PostId);
                    }  
                });
            }
        });
    });
</script>

but it just append the data together let me show you

image for status updating

what can i do to add the div every time the value goes through in the loop.

this is the html that i want to add in every div

<div class="post_status" id="post_status" style=" margin: 20px 50px 0px 40px; "> <a href="#" ><?php echo img($user_image); ?></a><a href=""><?php echo $uname; ?></a> <div class="status_post" id="status_data"></div> </div> 
  • 写回答

3条回答 默认 最新

  • dongwo5686 2017-02-20 07:50
    关注

    You need to append the whole generated html to the related container:

    $('<div class="post_status" id="post_status" style=" margin: 20px 50px 0px 40px; ">' +
        '<a href="#"><img src="'+status_image+'"></a><a href="">'+user_name+'</a>' + 
        '<div class="status_post" id="status_data">'+ post_status + '</div></div>')
     .appendTo("#the-comments-container");
    

    You need the user_name variable though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程