我在 jquery 中有代码,用于删除前一个 div 标签和下一个 .pclass。 这是我的代码:
$(".delete").bind("click",function(){
var c = confirm("You sure want to delete this?");
if(c){
/* $(this).next('.pclass').remove();
$(this).prev('.rurl').remove();
$(this).remove();*/
var text = $(this).prev('.rurl').text();
var idvalue = $(this).prev('.rurl').attr('id');
var id = idvalue.split("|");
$.ajax({
type: "POST",
url: "http://localhost:8080/cPEP_UI/Engine_rurl_delete",
data: "text="+text+"&eid="+id[1],
dataType: "json",
success: function(data) {
if(data.update == "success"){
$(this).next('.pclass').remove();
$(this).prev('.rurl').remove();
$(this).remove();
}
// $('#show').show();
//$('#show').html(data.update+" "+data.message).fadeOut(8000);
},
error:function(xhr,err){
//alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
$('#show').show();
// alert(xhr.responseText);
$('#show').html("responseText: "+xhr.responseText);
}
});
}
});
当我在调用 $.ajax 函数之前使用删除方法时,它工作正常,但当我将其放入 success 中时则不然。我已经检查了 servlet 文件返回的输出。运行良好。有什么想法吗?
最佳答案
您必须将上下文存储在变量中并将其传递给 success 函数,如下所示:
var that = $(this);
$.ajax({
...
success: function(data) {
that.remove();
}
});
或者,您可以使用 $.ajax
's context option :
$.ajax({
...
context: this, // set the context for all ajax-related callbacks
success: function(data) {
$(this).next('.pclass').remove();
}
});
关于jquery 不删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5649779/