如何使用甜蜜警报确认? [英] How to use confirm using sweet alert?
本文介绍了如何使用甜蜜警报确认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在此代码表单中提交,即使我点击否
In this code form is submitted even i am clicking on no
document.querySelector('#from1').onsubmit = function(){
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes, I am sure!',
cancelButtonText: "No, cancel it!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm){
if (isConfirm){
swal("Shortlisted!", "Candidates are successfully shortlisted!", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
};
推荐答案
您需要在提交时阻止默认表单行为。之后,您将需要以编程方式提交表格,以便选择确定按钮。
You will need to prevent default form behaviour on submit. After that you will need to submit form programmatically in case of Ok button is selected.
以下是它的样子:
document.querySelector('#from1').addEventListener('submit', function(e) {
var form = this;
e.preventDefault(); // <--- prevent form from submitting
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
icon: "warning",
buttons: [
'No, cancel it!',
'Yes, I am sure!'
],
dangerMode: true,
}).then(function(isConfirm) {
if (isConfirm) {
swal({
title: 'Shortlisted!',
text: 'Candidates are successfully shortlisted!',
icon: 'success'
}).then(function() {
form.submit(); // <--- submit form programmatically
});
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
})
});
UPD。上面的示例使用sweetalert v2.x promise API。
演示: http://plnkr.co/edit/YTY7PDs5Uh1XGUo9ic1s?p=preview
这篇关于如何使用甜蜜警报确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文