jquery 不删除元素

标签 jquery ajax

我在 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/

相关文章:

jquery - 如何仅针对特定请求显示ajax加载gif?

javascript - 无法选择 JQuery(移动)/JavaScript 选择选项

php - mysqli Select 无法正常工作

javascript - javascript `this.closest` 在 IE11 和 Chrome 中的使用

javascript - 扩展列表将其他相对定位的元素向上推——它们应该保持不动

javascript - 使用 jQuery 验证和 AJAX 的 Bootstrap 联系表单

jquery - 沿父div高度跟踪的 float div内容

javascript - Jquery Ajax 和 Json : How to check if undefined

javascript - 如何从JS函数获取数据到 Controller 类

jquery - 回调成功未调用,状态码为201