简体   繁体   English

DirtyForms与$ .blockUI无法正常工作

[英]DirtyForms does not work properly with $.blockUI

I'm using DirtyForms and $.blockUI plugin, the latter to change pages when clicking on links (in my app, some pages take a couple of seconds more to load and a visual feedback is fine). 我正在使用DirtyForms$ .blockUI插件,后者在点击链接时更改页面(在我的应用程序中,一些页面需要花费几秒钟才能加载并且视觉反馈很好)。

When I change field content and then click any link, DirtyForms is triggered: but when I cancel the process to stay on the page, $.blockUI starts its game, resulting in a stuck page 当我更改字段内容然后单击任何链接时,会触发DirtyForms:但是当我取消保留在页面上的进程时, $.blockUI启动它的游戏,导致页面卡住

 $('form[method="post"]').dirtyForms(); $('a').on('click', function(){ $.blockUI(); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.dirtyforms/2.0.0/jquery.dirtyforms.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.blockUI/2.70/jquery.blockUI.min.js"></script> <p>Change the field content to activate DirtyForms, then click on the link.<br> When the popup appears, click on "cancel" to stay on the page.<br> Watch blockUI getting fired as the link is going to be followed</p> <form action="#" method="post"> <input type="text" name="username" required> <button type="submit">send</button> </form> <a href="https://google.com">click me after changing field content</a> 

Please, any solution? 请问,任何解决方案?

EDIT: I also tried with stay.dirtyforms and afterstay.dirtyforms events, but they have no effect. 编辑:我也试图与stay.dirtyformsafterstay.dirtyforms事件,但没有任何效果。 defer.dirtyforms seems to work but the event is triggered twice (I put a console.log() to check) and I am not sure this is the way to go... defer.dirtyforms似乎工作,但事件被触发两次(我把一个console.log()检查),我不确定这是defer.dirtyforms ...

I've edit my answer : I've added some line of code to disable first the onbeforeunload dialog alert, taken from here . 我编辑了我的答案 :我已经添加了一些代码来禁用第一个onbeforeunload对话框警报, 取自此处 And at the end a link to an answer with another idea you can try. 最后,您可以尝试另一个想法的答案链接。

My idea: you have to prevent the default link action and use the $.blockUI Modal Dialogs methods to open a custom dialog, then catch the link attribute href from the link put it inside a variable and use the variable value for the #yes button of the dialog. 我的想法:您必须阻止默认链接操作并使用$ .blockUI 模式对话框方法打开自定义对话框,然后从链接中捕获链接属性href将其放入变量并使用#yes按钮的变量值对话框。

See if this solution can meet your needs 看看这个解决方案是否能满足您的需求

 /* beforeunload bind and unbind taken from https://gist.github.com/woss/3c2296d9e67e9b91292d */ // call this to restore 'onbeforeunload' var windowReloadBind = function(message) { window.onbeforeunload = function(event) { if (message.length === 0) { message = ''; }; if (typeof event == 'undefined') { event = window.event; }; if (event) { event.returnValue = message; }; return message; } }; // call this to prevent 'onbeforeunload' dialog var windowReloadUnBind = function() { window.onbeforeunload = function() { return null; }; }; var linkToFollow; // href to follow $('form[method="post"]').dirtyForms(); $('a').on('click', function(e){ e.preventDefault(); windowReloadUnBind(); // prevent dialog $.blockUI({ message: $('#question'), css: { width: '275px' } }); linkToFollow = $(this).attr('href'); }); $('#no').click(function() { $.unblockUI(); return false; }); $('#yes').click(function() { $(window.location).attr('href', linkToFollow); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.dirtyforms/2.0.0/jquery.dirtyforms.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.blockUI/2.70/jquery.blockUI.min.js"></script> <p>Change the field content to activate DirtyForms, then click on the link.<br> When the popup appears, click on "cancel" to stay on the page.<br> Watch blockUI getting fired as the link is going to be followed</p> <form action="#" method="post"> <input type="text" name="username" required> <button type="submit">send</button> </form> <a href="https://google.com" class="link">click me after changing field content</a> <div id="question" style="display:none; cursor: default"> <h6>Would you like to contine?.</h6> <input type="button" id="yes" value="Yes" /> <input type="button" id="no" value="No" /> </div> 

Other idea taken from another answer: Other idea would be to make a simple jQuery.ajax({}) call before return value in beforeunload as seen in this answer 从另一个答案中得出的其他想法其他想法是在beforeunload返回值之前进行简单的jQuery.ajax({})调用,如此答案中所示

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

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