dra87370 2015-09-21 12:02 采纳率: 100%
浏览 90

AJAX覆盖

I'm currently using AJAX for live submit a post which is then appended to a list. The problem I am having is when I submit a post more than once, AJAX overwrites the previous posts submitted.

var data = $("#form_write_post").serialize();
$.ajax({
    type: "POST",
    url: $("#form_write_post").attr("action"),
    data: data,
    beforeSend: function() {
        $("ul.timeline").prepend("<img class='textarea_ajax_loading' src='img/ajax-loader.gif' style='margin: 0 auto; display: block;' />");
    },
    complete: function() {
        $('.textarea_ajax_loading').remove();
    },
    success: function () {
        //var successCount = successCount++;
        $("ul.timeline").prepend('<li class=ajax_post></li>').fadeIn();
        $("ul.timeline .ajax_post").load("ajax_post.php").fadeIn();
        //$('ul.timeline').prepend(wall_post);
        //console.log("success");
        return false;
    }
});

How can I achieve a new post after each submission?

Thanks

  • 写回答

2条回答 默认 最新

  • doubi5127 2015-09-21 12:12
    关注

    I'm not sure that I fully understand your problem, but when you say "AJAX overwrites the previous posts submitted" I assume the problem is on the front-end right?

    From the code I see, it might be because you prepend a new <li class=ajax_post></li> and then you select all the li with this class and load something in all of them.

    If you want to load something only in the latest li, change the selector to

     $("ul.timeline .ajax_post:first").load("ajax_post.php").fadeIn();
    

    to select only the first post.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?