lin*_*ngo 6 javascript ajax jquery sweetalert
我是Javascript的新手 - 实际上是第一次编码.我正在尝试使用SweetAlert删除确认按钮.当我按下按钮时没有任何反应onclick="confirmDelete()".这段代码可能只是螃蟹,但这里是:
<script type="text/javascript">
function confirmDelete() {
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
)},
$.ajax({
url: "scriptDelete.php",
type: "POST",
data: {id: 5},
dataType: "html",
success: function () {
swal("Done!","It was succesfully deleted!","success");
}
});
}
</script>
<a href="#" onclick="confirmDelete()">Delete</a>
Run Code Online (Sandbox Code Playgroud)
如果删除失败,我可以添加任何警报吗?
Dhi*_*raj 13
如果我正确理解你的问题,你就会问如何处理ajax请求中的错误条件.Ajax设置有一个错误属性,它可以像这样使用
$.ajax({
.... other settings you already have
error: function (xhr, ajaxOptions, thrownError) {
swal("Error deleting!", "Please try again", "error");
}
});
Run Code Online (Sandbox Code Playgroud)
此外,您正在以错误的方式调用swal.Swal有这样的回调
swal({settings}, function(isConfirm){});
Run Code Online (Sandbox Code Playgroud)
整体代码看起来像这样
function confirmDelete() {
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 (isConfirm) {
if (!isConfirm) return;
$.ajax({
url: "scriptDelete.php",
type: "POST",
data: {
id: 5
},
dataType: "html",
success: function () {
swal("Done!", "It was succesfully deleted!", "success");
},
error: function (xhr, ajaxOptions, thrownError) {
swal("Error deleting!", "Please try again", "error");
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
这是一个演示http://jsfiddle.net/dhirajbodicherla/xe096w10/33/
试试这个代码。它对我来说很好用。
$('.delete-confirm').on('click', function() {
var postID = $(this).val();
console.log(postID);
swal({
title: "Are you sure?",
text: "If you delete this post all associated comments also deleted permanently.",
type: "warning",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes, delete it!",
}, function() {
setTimeout(function() {
$.post("delete.php", {
id: postID
},
function(data, status) {
swal({
title: "Deleted!",
text: "Your post has been deleted.",
type: "success"
},
function() {
location.reload();
}
);
}
);
}, 50);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43632 次 |
| 最近记录: |