使用自定义模型弹出确认表单提交
Confirming a form submission with a custom model popup
我的应用程序中有一些删除表单,我想在提交前用javascript/jQuery确认。
简单的方法是这样做:
$('form.confirm-form').submit(function(){
return confirm('Are you sure?');
});
会弹出一个浏览器自带的确认框,只有当用户单击OK时才提交表单。
对于更漂亮的东西,我想我使用Bootstrap模式进行确认,并为此检查了Bootbox.js库。
我似乎不能让它正常工作,因为它需要一个回调,不阻止表单提交。我用preventDefault
来阻止表单过早提交但是现在
- 我不能用取消按钮取消模式,
- OK按钮提交表单失败。
下面是我的代码:
$('form.confirm-form').submit(function(event) {
event.preventDefault();
bootbox.confirm("Are you sure?", function(result){
return result;
});
});
我如何用这个库复制本机confirm
的行为?
我在一个创建表单上挣扎,解决方案实际上很容易,根本不需要任何JavaScript(如果你已经在使用JQuery和Bootstrap)。
你所需要做的就是将提交按钮(button type="submit")更改为触发Boostrap模式的通用按钮(button type="button")。更多关于按钮动作的信息:https://stackoverflow.com/a/9643866.
然后让Boostrap模式中的按钮实际提交表单。如果你把你的模态HTML保存在表单中,你不需要写一行JS。
JS Bin: https://jsbin.com/lulipubafo/edit?html,output
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<title>JS Bin</title>
</head>
<body>
<form action="/page" method="post">
<input type="text" name="first_name">
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#confirm-submit">Submit</button>
<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title">Confirm</h2>
</div>
<div class="modal-body">
<p>Are you sure you want to submit?</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">Yes, Submit</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
试试这个
$('form.confirm-form').submit(function(event) {
var currentForm = this;
event.preventDefault();
bootbox.confirm("Are you sure?", function(result){
if(result)
{
currentForm.submit()
}
});
});
相关文章:
- 为什么这个JS确认窗口没有在表单提交时启动
- 取消“确认表单重新提交”对话框
- 如何停止'确认表单重新提交'对话框,同时刷新页面
- 如何在用户尝试刷新浏览器时获取“确认表单重新提交”
- AJAX 表单提交确认消息
- 表单提交-确认
- Ajax表单提交&Javascript确认错误
- 如何使用JavaScript确认对话框菜单触发表单提交事件
- 使用引导程序验证程序进行验证后,停止表单提交并打开引导模式(确认对话框)
- js表单验证-确认表单提交时没有发现错误
- JQUERY模态框确认表单提交
- 如何创建自定义对话框以确认表单提交
- Javascript表单提交确认与重定向,如果为真
- 剃刀表单2提交按钮与确认
- JavaScript表单提交-确认或取消提交对话框
- 隐藏确认表单重新提交PHP
- Javascript确认表单提交而不是取消
- 确认表单重新提交绕过
- 表单提交时的PHP确认消息
- 使用自定义模型弹出确认表单提交