[英]Bootstrap modal on form submit not displaying at correct point
Requirement要求
I want to display a fixed message to the user whilst a file is being uploaded and attached to an email to discourage clicking back or refreshing the page or repeatedly clicking the submit button.我想在上传文件并附加到电子邮件时向用户显示一条固定消息,以阻止单击返回或刷新页面或重复单击提交按钮。
Background背景
I'm using bootstrap v3.3.7 and JQuery v3.3.1.我正在使用引导程序 v3.3.7 和 JQuery v3.3.1。
I thought a good way to tackle this requirement was to make use of the Bootstrap Modal plugin, especially when I realised you could fix the modal on screen using:我认为解决这个要求的一个好方法是使用 Bootstrap Modal 插件,特别是当我意识到你可以使用以下方法修复屏幕上的模态时:
$("#modal-foo").modal({backdrop: 'static',keyboard: false});
I have the following modal:我有以下模式:
@model Web.ViewModels.Site.Foo.FooViewModel
<div class="modal fade" id="modal-foo" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title" id="modal-label">
<i class="fa fa-spin fa-spinner"></i> Uploading your file and submitting it for review
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<p>We're currently uploading and submitting your file for review.</p>
<p>This may take a few moments. Please do not hit the back button or try to refresh this page whilst this is happening.</p>
</div>
</div>
</div>
</div>
I have the following form:我有以下表格:
<form id="fooform" asp-action="fooaction" asp-controller="foocontroller" asp-route-fooid="@Model.FooId" method="post" enctype="multipart/form-data" class="form-horizontal">
I have the following button:我有以下按钮:
<input type="submit" value="Submit" class="btn btn-primary" />
And the following jquery:以及以下jquery:
//when dom loaded hooks
$.when($.ready).then(function () {
$('#fooform').on('submit', function () {
if ($(this).valid()) {
//we've passed the unobtrusive validation so now present the message to the user
$("#modal-foo").modal({
backdrop: 'static',
keyboard: false
});
}
});
})
Issue问题
What I was hoping would happen is that the modal would appear once the unobtrusive validation has passed, and is displayed until the user is redirected to a thank you page.我希望会发生的是,一旦不显眼的验证通过,模态就会出现,并一直显示到用户被重定向到感谢页面。
What I'm finding happen is that the modal isn't appearing until later in the process.我发现发生的是模态直到过程后期才出现。
For example, if I put some alerts in to see what's happening, using the code below, the modal doesn't appear until after 'here3'.例如,如果我放入一些警报以查看发生了什么,使用下面的代码,模态直到“here3”之后才会出现。 It's as though the .modal is firing an instruction to browser to display the modal but it doesn't action it immediately.
就好像 .modal 正在向浏览器发出指令以显示模态,但它不会立即执行操作。
//when dom loaded hooks
$.when($.ready).then(function () {
$('#fooform').on('submit', function () {
alert('here1');
if ($(this).valid()) {
//we've passed the unobtrusive validation so now present the message to the user
alert('here2');
$("#modal-foo").modal({
backdrop: 'static',
keyboard: false
});
alert('here3');
}
});
})
Alternative attempt to achieve requirement达到要求的替代尝试
I've tried using an anchor instead of a submit button but again, the modal doesn't appear until after 'here3':我尝试使用锚点而不是提交按钮,但同样,直到“here3”之后才会出现模式:
<a class="btn btn-primary" onclick="showModal();">Submit</a>
Javascript function: Javascript函数:
function showModal() {
if ($('#fooform').valid()) {
//we've passed the unobtrusive validation so now present the message to the user
alert('here1');
$("#modal-foo").modal({
backdrop: 'static',
keyboard: false
});
alert('here2');
//can I somehow wrap the submit in a promise or delegate so that after the modal appears it does the submit?
$('#fooform').submit();
alert('here3');
}
}
Question题
How can I achieve my requirement?我怎样才能达到我的要求? As per my comment in the Javascript function above, can I wrap the form submit in a promise or delegate or can I set something on the DOM to fire after the modal has displayed or is there an alternative to the Modal that I should look at?
根据我在上面的 Javascript 函数中的评论,我可以将提交的表单包装在承诺或委托中,还是可以在 DOM 上设置一些内容以在模式显示后触发,或者是否有替代模式我应该查看?
The problem is while you click the submit button your page reloading that's why you cannot see your modal问题是当您单击提交按钮时,您的页面会重新加载,这就是您看不到模态的原因
$.when($.ready).then(function () {
$('#fooform').on('submit', function (e) {
e.preventDefault();//added here
// if ($(this).valid()) {
//we've passed the unobtrusive validation so now present the message to the user
$("#modal-foo").modal({
backdrop: 'static',
keyboard: false
});
// }
});
})
see your above code i used 'e.preventDefault()' to block your submission on click Now you can see your model But your 'valid()' command will not work here, because form is prevented from submitting, that's why i commented out that....看到你上面的代码,我用'e.preventDefault()'来阻止你点击提交现在你可以看到你的模型但是你的'valid()'命令在这里不起作用,因为表单被阻止提交,这就是我注释掉的原因那....
So change your code according to this.....所以根据这个改变你的代码......
One method is using AJAX you can send data without page reload... otherwise give a button to open model and put submit button on the model...一种方法是使用 AJAX,您可以在不重新加载页面的情况下发送数据......否则给一个按钮打开模型并将提交按钮放在模型上......
There is one more chance that model will not work...模型还有一次可能不起作用......
while inserting scripts it must be in order that jquery must insert before other plugins, insert,在插入脚本时,它必须是为了让 jquery 必须在其他插件之前插入,插入,
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
above code first, then try,先上面的代码,然后尝试,
i am including the working full code below我包括下面的工作完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
</head>
<body>
@model Web.ViewModels.Site.Foo.FooViewModel
<div class="modal fade" id="modal-foo" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title" id="modal-label">
<i class="fa fa-spin fa-spinner"></i> Uploading your file and submitting it for review
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<p>We're currently uploading and submitting your file for review.</p>
<p>This may take a few moments. Please do not hit the back button or try to refresh this page whilst this is happening.</p>
</div>
</div>
</div>
</div>
<form id="fooform" asp-action="fooaction" asp-controller="foocontroller" asp-route-fooid="@Model.FooId" method="post" enctype="multipart/form-data" class="form-horizontal">
</form>
<a class="btn btn-primary" onclick="showModal();">Submit</a>
</body>
<script>
function showModal() {
if ($('#fooform').valid()) {
//we've passed the unobtrusive validation so now present the message to the user
alert('here1');
$("#modal-foo").modal({
backdrop: 'static',
keyboard: false
});
alert('here2');
//can I somehow wrap the submit in a promise or delegate so that after the modal appears it does the submit?
// $('#fooform').submit();
alert('here3');
}
}
</script>
</html>
In the end I got this working by using the following.最后,我通过使用以下内容来完成这项工作。 Hopefully, someone else may find this useful:
希望其他人可能会发现这很有用:
Same button and form相同的按钮和表单
<input type="submit" value="Submit" class="btn btn-primary" />
<form id="fooform" asp-action="fooaction" asp-controller="foocontroller" asp-route-fooid="@Model.FooId" method="post" enctype="multipart/form-data" class="form-horizontal">
Modal模态
Note the additional data-backdrop
and data-keyboard
attributes on the 1st div请注意第 1 个 div 上的附加
data-backdrop
和data-keyboard
属性
@model Web.ViewModels.Site.Foo.FooViewModel
<div data-backdrop="static" data-keyboard="false" class="modal fade" id="modal-foo" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title" id="modal-label">
<i class="fa fa-spin fa-spinner"></i> Uploading your file and submitting it for review
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<p>We're currently uploading and submitting your file for review.</p>
<p>This may take a few moments. Please do not hit the back button or try to refresh this page whilst this is happening.</p>
</div>
</div>
</div>
</div>
jQuery jQuery
I ended up taking the key elements from the modal plugin in the Bootstrap js file and the modal appeared at the point I expected it to whilst still giving me the modal display effects that I was looking for.我最终从 Bootstrap js 文件中的模态插件中获取了关键元素,模态出现在我预期的点上,同时仍然为我提供了我正在寻找的模态显示效果。
function showModal() {
var $body = $("body");
$body.addClass("modal-open");
$(document.createElement('div'))
.addClass('modal-backdrop fade in')
.appendTo($body)
var $modal = $("#modal-foo");
$modal.show();
$modal.scrollTop(0);
$modal[0].offsetWidth;
$modal.addClass('in');
}
//when dom loaded hooks
$.when($.ready).then(function () {
$('#fooform').on('submit', function () {
if ($(this).valid()) {
showModal();
}
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.