我的 Ajax 方法如下所示
$.post(url,{
ajax_call:"putDonation",
addresse:addresse,
phone:phone,
email:email,
name:name,
amount:amount,
amountinwords:amountinwords
}).done(function(data){
console.log(data);
var arr = [];
try {
var str = "";
//convert to json string
arr = $.parseJSON(data); //convert to javascript array
$.each(arr,function(key,value){
str +="<li>"+value+"</li>";
});
alert(str);
$(".alert-danger").show();
$("#error").html(str);
} catch (e) {
swal("Jay Gayatri !", 'Donation Sucessful', "success")
$("#donation")[0].reset();
}
})
我想显示一个像这样的甜蜜警报警告弹出窗口
swal({
title: "Are you sure ?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
},
function(){
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
如果他们单击“取消”,则不应执行 ajax 调用,如果他们选择"is",则只应发生调用
谁能告诉我如何将 Ajax 方法嵌入到 Sweet Alert
方法中
谢谢
最佳答案
作为一个简单的例子,我可以向您展示我是如何在我的网站上做到这一点的。我将 Ajax 调用放在甜蜜的警报中。
function deleteorder(orderid) {
swal({
title: "Are you sure?",
text: "Are you sure that you want to cancel this order?",
type: "warning",
showCancelButton: true,
closeOnConfirm: false,
confirmButtonText: "Yes, cancel it!",
confirmButtonColor: "#ec6c62"
}, function() {
$.ajax(
{
type: "post",
url: "/admin/delete_order.php",
data: "orderid="+orderid,
success: function(data){
}
}
)
.done(function(data) {
swal("Canceled!", "Your order was successfully canceled!", "success");
$('#orders-history').load(document.URL + ' #orders-history');
})
.error(function(data) {
swal("Oops", "We couldn't connect to the server!", "error");
});
});
}
因此,仅当您按下确认按钮时才会进行 ajax 调用。我希望这可以帮助您按照您需要的方式安排代码。
关于jquery - 如何添加带有甜蜜警报的 Ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33388492/