【问题标题】:Close Modal on clicking outside the modal only if there are no changes made in form仅当表单未进行任何更改时,单击模态外部时关闭模态
【发布时间】:2015-03-19 20:09:27
【问题描述】:

我有一个模态框,在模态框外部单击时我需要关闭模态框,前提是模态框没有任何更改。我有点能够实现它。我已经在单击模态和在模态之外编写了代码。但我只需要在模态外单击时触发该功能。

$(".modal").on('click',function() { 
    if(changed_data!= original_data)
      {
         var result = confirm("Are you sure ?");
         if(result == true){
             $('#dialog').data('bs.modal').options.backdrop = true;
             $('#form').removeData('bs.modal');
         }else{
             $('#dialog').data('bs.modal').options.backdrop = 'static';
         }
      }else{
            $('#dialog').data('bs.modal').options.backdrop = true;
            $('#form').removeData('bs.modal');
          }
 });

现在我只需要在模态外部单击时调用此函数。我找到了一个选项 hide.bs.modal, hidden.bs.modal 但它们不符合我的要求。如果我使用它们,当我再次打开模态时,应用于模态的更改会显示效果。有什么建议吗?

【问题讨论】:

    标签: twitter-bootstrap-3 modal-dialog bootstrap-modal jquery-dialog


    【解决方案1】:

    在显示模态后添加click事件,锁定模态以防止其关闭,使用您的功能,如果可以关闭,则解锁并关闭它

    $('#myModal').on('shown.bs.modal', function () {
        $('#myModal').modal('lock');
        $('#myModal').on('click', function(e) {
            //do nothing if you're clicking inside the actual modal      
            if((' ' + e.target.className + ' ').indexOf(' ' + 'modal-dialog' + ' ') > -1 || $(e.target).closest('.modal-dialog').length)
                return;
    
            if(changed_data != original_data)
            {
                var result = confirm("Are you sure ?");
                if(result == true){
                    $('#myModal').modal('unlock').modal('hide').off("click");
                }
            }else{
                $('#myModal').modal('unlock').modal('hide').off("click");
            }
        });
    });
    

    编辑

    哦,我记得我使用了lockunlock,因为我正在扩展我的基本模态以拥有这些。要允许锁定和解锁与您的模态一起使用,请在 $(document).ready 中使用此代码

    // save the original function object
    var _superModal = $.fn.modal;
    
    // add locked as a new option
    $.extend( _superModal.Constructor.DEFAULTS, {
        locked: false
    });
    
    // capture the original hide
    var _hide = _superModal.Constructor.prototype.hide;
    
    // add the lock, unlock and override the hide of modal
    $.extend(_superModal.Constructor.prototype, {
        // locks the dialog so that it cannot be hidden
        lock: function() {
            this.options.locked = true;
        }
        // unlocks the dialog so that it can be hidden by 'esc' or clicking on the backdrop (if not static)
        ,unlock: function() {
            this.options.locked = false;
        }
        // override the original hide so that the original is only called if the modal is unlocked
        ,hide: function() {
            if (this.options.locked) return;
                _hide.apply(this, arguments);
        }
     });
    

    【讨论】:

    • 嗨 FabioG modal-dialog 在 if 语句中指定了什么
    • 它正在检查您是否没有在实际模式内部单击而不是在外部单击。 .modal 是父类,它包括模态框的背景和实际对话框,.modal-dialog 是模态框本身的类
    • 嗨 FabioG,您建议在显示模态时编写代码,但在关闭模态之前显示模态后我会得到更改的数据。如果我使用您的代码,我将获得相同的原始数据和更改数据
    • 你如何设置changed_data?顺便说一句,还有original_data
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2020-07-30
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多