,可以通过以下步骤实现:
以下是一个示例代码:
<!DOCTYPE html>
<html>
<head>
<title>阻止表单提交示例</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/sweetalert2@11.1.4/dist/sweetalert2.min.css">
</head>
<body>
<form id="myForm" action="submit.php" method="POST">
<input type="text" name="name" placeholder="姓名" required>
<input type="email" name="email" placeholder="邮箱" required>
<button type="submit">提交</button>
</form>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.1.4/dist/sweetalert2.all.min.js"></script>
<script>
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault(); // 阻止表单默认提交行为
Swal.fire({
title: '确认提交表单?',
icon: 'question',
showCancelButton: true,
confirmButtonText: '确认',
cancelButtonText: '取消'
}).then((result) => {
if (result.isConfirmed) {
// 用户确认提交表单
event.target.submit(); // 执行表单提交操作
} else {
// 用户取消提交表单
Swal.fire('已取消', '表单未提交', 'info');
}
});
});
</script>
</body>
</html>
在上述示例中,我们使用了SweetAlert2库来创建一个弹窗,询问用户是否确认提交表单。如果用户点击确认按钮,则执行表单的提交操作;如果用户点击取消按钮,则显示一个提示信息,表单不会被提交。
这个示例中使用的是SweetAlert2库,它是一个强大且易于使用的弹窗插件。你可以根据实际需求选择其他类似的弹窗库或自行实现弹窗效果。
注意:以上示例中的表单提交地址为"submit.php",你需要根据实际情况修改为你的后端处理表单提交的地址。
领取专属 10元无门槛券
手把手带您无忧上云