我想暂停表单的提交,所以我可以让用户在继续之前确认操作。 因此,这里是我的形式:SweetAlert与Java Servlet
<form action="<%= request.getContextPath()%>/Controller"
method="POST" id="remove_book"
onsubmit="alertBookInfo('return_book')">
</form>
然后,我创建JavaScript函数,使用SweetAlert:
function alertInfo(action) {
document.querySelector(action).addEventListener('submit', function (e) {
var form = this;
e.preventDefault();
if (action === "remove_book") {
swal({
title: "Remove book?",
text: "Watch out",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes.",
cancelButtonText: "No.",
closeOnConfirm: false,
closeOnCancel: false
},
function (isConfirm) {
if (isConfirm) {
swal({
title: "Deleted.",
text: "Done.",
type: "success"
}, function() {
form.submit();
});
} else {
swal("Cancelled", "Not done.", "error");
}
});
}
});
}
但由于某些原因,我无法阻止对表单页面重载提交。我在做错什么? PS:我已经尝试在窗体中返回alertInfo(),并从JS函数返回布尔值,但没有成功。
哪里是你的函数'alertBookInfo( 'return_book')'? – Zorken17
在我的文件“functions.js”中,正确链接。 – pietrochico