简体   繁体   English

提交表单后立即显示模式(验证之前)

[英]Instantly display a modal when the form is submitted (before validation)

I'm using a bootstrap validator from ( https://github.com/1000hz/bootstrap-validator/blob/master/js/validator.js ) and I'm trying to instantly display a modal 'loading' box when the submit button is pressed on a form. 我正在使用来自( https://github.com/1000hz/bootstrap-validator/blob/master/js/validator.js )的引导程序验证器,并且尝试在提交时立即显示模式“加载”框在窗体上按下按钮。 I've achieved this by doing the following: 我已经做到了以下几点:

$('form').on('submit', function (event) {
    showLoadingModal();
    if (!event.isDefaultPrevented()) {
        event.preventDefault();
        submitForm(this);
    } else {
        hideLoadingModal();
    }
});

However I'm getting a problem where there is a small gap of time (under a second) between clicking the button and the modal being displayed. 但是我遇到了一个问题,即单击按钮和显示的模态之间的时间间隔很小(不到一秒钟)。 I'm assuming this delay is caused by the time taken validating all the fields on the form of which there are quite a lot. 我假设此延迟是由于验证表单上的所有字段所花费的时间很多而造成的。

This therefore leads me to believe that the validator 'form submit' is being executed before my code and I should be doing something different to call the showLoadingModal() 因此,这使我相信验证程序“表单提交”正在我的代码之前执行,并且我应该做一些不同的事情来调用showLoadingModal()

Edit: 编辑:

I've added some logging into the js to work out what happens and when. 我在js中添加了一些日志记录,以了解发生的情况和时间。 I've also moved the showLoadingModal() into a 'button clicked' event to ensure it happens before form submit. 我还将showLoadingModal()移到了“单击按钮”事件中,以确保它在表单提交之前发生。 Here's the order my messages get displayed: 这是我的消息显示的顺序:

button clicked 点击按钮
before show modal 演出前的模态
after show modal 秀后模态
form submitted 表格已提交
about to validate 即将验证
<--Modal appears now--> <-模态现在出现->

set a delay of 300ms before submitting your form 在提交表格之前设置300ms的延迟

var event;
var formobj;
('form').on('submit', function (event) {
showLoadingModal();
event = event; //save event to be used later 
formobj = this;
setTimeout(function() 
{
     if (!event.isDefaultPrevented()) {
        event.preventDefault();
        submitForm(formobj );
      } else {
          hideLoadingModal();
      }
},300);
return false; //Prevent normal submission of the form so that the dialog box is visible
});

I am assuming submitForm(this) is the function that does the form posting causing the page to reload 我假设submitForm(this)是执行表单发布导致页面重新加载的函数

Just a thought, hope it helps! 只是一个想法,希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM