24 javascript jquery
在这个代码表格提交,即使我点击否
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");
}
});
};
Run Code Online (Sandbox Code Playgroud)
dfs*_*fsq 36
您需要在提交时阻止默认表单行为.之后,如果选择了"确定"按钮,则需要以编程方式提交表单.
这是它的样子:
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");
}
})
});
Run Code Online (Sandbox Code Playgroud)
UPD.上面的示例使用sweetalert v2.x promise API.
演示: http ://plnkr.co/edit/YTY7PDs5Uh1XGUo9ic1s?p=preview
小智 9
我在使用 SweetAlert2 时也遇到了这个问题。SA2 与 1 不同,它将所有内容都放入结果对象中。上面的内容可以通过下面的代码来完成。
Swal.fire({
title: 'A cool title',
icon: 'info',
confirmButtonText: 'Log in'
}).then((result) => {
if (result['isConfirmed']){
// Put your function here
}
})
Run Code Online (Sandbox Code Playgroud)
放置在 then 结果中的所有内容都将运行。结果包含几个可用于实现此目的的参数。非常简单的技术。不确定它在 SweetAlert1 上是否同样有效,但我真的不知道为什么你会选择新版本之上的那个。
document.querySelector('#from1').onsubmit = function(e){
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");
e.preventDefault();
}
});
};
Run Code Online (Sandbox Code Playgroud)
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!"
}).then(
function () { /*Your Code Here*/ },
function () { return false; });
Run Code Online (Sandbox Code Playgroud)