我正在使用jQuery将ajax内容加载到页面上。但是,尝试使用jQuery的.remove()
函数似乎无法删除动态加载的元素。
我的示例功能:
function deletePerson(personID){
if (confirm("Are you sure you want to permanently delete this person?\nYou cannot undo this action.")){
$.post("ajax/personsDelete.php", { personNo: personID },
function(data){
$('#person' + personID).remove();
alert("Response: " + data);
});
}
}
如何使用jQuery删除动态加载的元素?
答案 0 :(得分:3)
更改选择器的上下文以查看响应,添加,data
:
function deletePerson(personID){
if (confirm("Are you sure you want to permanently delete this person?\nYou cannot undo this action.")){
$.post("ajax/personsDelete.php", { personNo: personID },
function(data){
$('#person' + personID, data).remove();
alert("Response: " + data);
});
}
}
由于它尚未添加到文档中,因此您调用的默认选择器将无法找到它:
$('#person' + personID)
与$('#person' + personID, document)