gpt4 book ai didi

javascript - 如何使用 Sweet Alert 进行确认?

转载 作者:行者123 更新时间:2023-12-03 21:32:25 24 4
gpt4 key购买 nike

在此代码中,即使我单击“否”,也会提交表单

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");
}
});
};

最佳答案

您需要防止提交时的默认表单行为。之后,如果选择“确定”按钮,您将需要以编程方式提交表单。

它可能是这样的:

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

关于javascript - 如何使用 Sweet Alert 进行确认?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31136889/

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