我正在尝试做一个待办事项列表类型的东西。在这里,我希望将新目标保存到数据库中,然后仅刷新具有相关列表的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.
});
});
请参阅
this
在回调中访问正确的/上下文?对于一般问题的解释和解决方案,以及$.ajax
但由于标识应该是在HTML文档反正独特,可以替换$(this).closest('#goallist')
使用$('#goallist')
,完全忽略this
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句