【发布时间】:2017-01-25 09:39:14
【问题描述】:
我想暂停表单的 onsubmit,以便在继续之前要求用户确认操作。 所以这是我的表格:
<form action="<%= request.getContextPath()%>/Controller"
method="POST" id="remove_book"
onsubmit="alertBookInfo('return_book')">
</form>
然后,我使用 SweetAlert 创建了 JavaScript 函数:
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:我已经尝试在表单中使用 return alertInfo() 并从 JS 函数返回布尔值,但没有成功。
【问题讨论】:
-
你的函数
alertBookInfo('return_book')在哪里? -
在我的文件“functions.js”中,正确链接。
标签: javascript java jquery html sweetalert