我想暫停表單的提交,所以我可以讓用戶在繼續之前確認操作。 因此,這裏是我的形式: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