如何在 bpopup 中的 beforeclose 事件上调用函数?

How to call a function on beforeclose event in bpopup?

我想在关闭 bpopup 之前调用一个函数。我使用了以下代码:

$('#casepromptPopup').bPopup({
    escClose: false,
    closeClass: 'b-close',
    modalClose: false,
    onClose: function() {                         
        confirm("Are you sure you wish to quit and grade your performance?");

我想在弹出窗口关闭前显示确认消息。但是 onClose 方法在弹出窗口关闭后触发。

虽然我不喜欢插件修改,但似乎没有其他方法可以做到这一点。

修改bpopup.js:

  • 1. 找到 function close() 并添加 if(o.confirmClose()){ :

    function close() {
        if(o.confirmClose()){
            // rest of the original function
        }
    }
    
  • 2. 找到 $.fn.bPopup.defaults = {(在底部)并添加:

    confirmClose: function(){return true;}
    

现在您可以在 bpopup 选项中添加确认:

$('#casepromptPopup').bPopup({
    escClose     : false,
    closeClass   : 'b-close',
    confirmClose : function(){                         
        return confirm("Are you sure you wish to quit and grade your performance?");
    }
});

JSFiddle