gpt4 book ai didi

javascript - 如何访问$(this)里面sweetalert,里面AJAX成功

转载 作者:行者123 更新时间:2023-11-27 22:38:54 25 4
gpt4 key购买 nike

我想删除被单击元素的父元素。我使用 Sweet Alert 来首先获取警报,然后在调用 AJAX 函数之后我想获取 success 函数中的元素:

这是我的功能:

 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内部的这个上下文。所以,我们需要将这个上下文传递给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");
}
}
});

}
);
});

关于javascript - 如何访问$(this)里面sweetalert,里面AJAX成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38907136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com