2015-04-07 43 views
0

我有一個叫做新聞的div類。它包含用戶的一些更新。當用戶提交新的更新時,我希望這個新的更新出現在新聞類的頂部,因爲它是新的。加載ajax成功的php文件

我嘗試了以下方法,但它不能按我想要的方式工作。當我提交表單,latestpost.php被載入但新聞DIV消失(因爲我的前置()是空的。)

$(document).ready(function() { 
    $('#new').submit(function (e) { 
     e.preventDefault(); 
     var form = $(this); 
     var formData = form.serialize(); 
     $.ajax({ 
      type: 'POST', 
      url: '/modules/feed/process.php', // the url where we want to POST 
      data: formData, // our data object 
      success: function() { 
       $("#statustext").val(''); // I reset textarea's value here 
       $(".news").prepend().load("/modules/feed/latestpost.php"); // problematic part 
      } 
     }); 
    }); 
}); 
+2

您可以在process.php – brenjt

回答

3

試用​​代$.ajax(),利用context設置與$(".news")作爲價值

$.ajax({ 
    type:"GET", 
    url:"/modules/feed/latestpost.php", 
    context:$(".news") // `$(".news")` : `this` within `.then()` 
}) 
.then(function(data) { 
    this.prepend(data); // `this`:`$(".news")` 
}, function(jqxhr, textStatus, errorThrown) { 
    console.log(errorThrown) 
}); 
+0

@salep的回覆中返回最新帖子歡迎您:) – guest271314