我想做一個待辦事項列表類型的事情。 在這裏,我希望將新目標保存到數據庫,然後僅對具有相關列表的div進行刷新,並顯示添加了新目標的列表。
數據庫更新正常,新的列表項返回新的HTML就好了,但 .replaceWith()似乎不工作。什麼都沒發生。
它的工作原理是功能之外,而不是裏面的話,我不知道爲什麼。
$('.addGoal').submit(function(event){
var listid = $(this).attr('id');
var desc = $(this).children('.goalinput').val();
event.preventDefault();
if(desc == "") {
console.log("empty!");
return;
}
var posting = $.post("updatedata.php", { what :'addgoal', id: listid, data: desc});
posting.done(function(data){
console.log("saved!" + data); //this works, the updated html is being returned
$(this).closest('#goallist').replaceWith("Returned data goes here"); //this works outside of this function but not inside of it.
});
});
謝謝!它不僅現在完美地工作,而且我總體上受到很高的教育。 – Cactuar