代码之家  ›  专栏  ›  技术社区  ›  Ivan

dirtyforms不能与$.blockui一起正常工作

  •  4
  • Ivan  · 技术社区  · 7 年前

    我在用 DirtyForms $.blockUI 插件,后者可以在点击链接时更改页面(在我的应用程序中,有些页面需要几秒钟的时间才能加载,视觉反馈也很好)。

    当我更改字段内容,然后单击任何链接时,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>

    拜托,有什么解决办法吗?

    stay.dirtyforms afterstay.dirtyforms 事件,但没有效果。 defer.dirtyforms 似乎有效,但事件被触发了两次(我将 console.log() 我不确定这是不是一条路…

    1 回复  |  直到 7 年前
        1
  •  2
  •   Emanuele    7 年前

    我已经修改了我的答案 :我已经添加了一些代码行,以首先禁用 onbeforeunload 对话框警报, taken from here . 在最后一个链接到一个答案与另一个想法,你可以尝试。

    我的想法: 您必须阻止默认链接操作并使用$.blockui Modal Dialogs methods 若要打开自定义对话框,请捕获链接属性 href 从链接将其放在变量中,并将变量值用于 #yes 对话框的按钮。

    看看这个解决方案是否能满足您的需要

    /* 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>

    另一种观点来自另一个答案: 另一个想法是 jQuery.ajax({}) 返回值前调用 beforeunload as seen in this answer

    推荐文章