如何访问 $(this) inside sweet alert,inside AJAX success

how to access the $(this) inside sweet alert, inside AJAX success

我想删除点击元素的父元素。我首先使用甜蜜警报来获得警报,然后在调用 AJAX 函数之后我想获取成功函数中的元素:

这是我的功能:

 function removeImage(id) {  
        var element = $(this).closest('.dropzone');

    swal({   
      title:"Delete",   
      text: "delete",  
      type: "warning",   
      showCancelButton: true,   
      confirmButtonColor: "#DD6B55",   
      confirmButtonText: "Yes !",   
      cancelButtonText: "No, cancel",   
      closeOnConfirm: false,   closeOnCancel: false 
    }, function(isConfirm){
      if (isConfirm) {
        $.ajax({
          type: "POST", 
          data: {id: id},
          url:'ajax/deleteimage.php',
          success : function(data){ 
            if(data == 'ok'){
                    swal({   
                title:"Delete",   
                text: "delete",  
                type: "success",    
                confirmButtonColor: "#AEDEF4",
                confirmButtonText: "Ok",   
                closeOnConfirm: true,   
              }, function(isConfirm){
                    $.when($('.dropzone').find("#"+id).parent().fadeOut())
                                       .done(function() {
                          $('.dropzone').find("#"+id).parent().remove();
                      });
                      var n_div = $('.dz-image-preview').length-1;
                          if (n_div === 0) {
                             $('.dz-message').css("opacity",'1');
                          }
              });
            }else{
              swal("Error, try again", "", "error");  
            }  
               }
        }); // end ajax call 

      } else {     
        swal("Cancel", "", "error");   
      } 
    }); 


  }

我无法用变量元素更改 success 函数中的 $('.dropzone')

我在 jQuery 遇到了同样的问题。主要问题是ajax不能直接访问swal内部的this context。因此,我们需要将此上下文传递给 ajax。为此,我们首先将此上下文存储在一个变量中,然后在 ajax.

中分配该上下文
$(".delete_category").click(function () {
    var call_url = $(this).val();
    var this_context = $(this);
    swal({
        title: "Are you sure?",
        text: "Your may not be able to recover!",
        type: "warning",
        showCancelButton: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "Yes, delete it!",
        closeOnConfirm: false
    },
        function () {
            $.ajax({
                context: this_context,
                method: "POST",
                url: call_url,
                success: function (response) {
                    if (response == "success") {
                        swal("Deleted!", "Record deleted successfully.", "success");
                        $(this).closest('.delete_row').hide();
                    } else {
                        swal("Error!", response, "error");
                    }
                }
            });

        }
    );
});