确认关闭Colorbox

Kin*_*ien 3 javascript colorbox

如果用户试图关闭colorbox,我想显示简单的确认弹出框.我试过这段代码:

onCleanup:function(){ confirm('Are you sure'); }
Run Code Online (Sandbox Code Playgroud)

显示确认框,但即使点击"取消",彩盒也关闭!

有人可以帮忙吗?

bfn*_*ncs 9

彩盒常见问题解答中已有一个很好的例子

你必须重新定义colorbox'close方法:

var originalClose = $.colorbox.close;
$.colorbox.close = function(){
  if (confirm('Do you want to close this window?')) {
    originalClose();
  }
};
Run Code Online (Sandbox Code Playgroud)


Ken*_*ley 5

我和FancyBox做过类似的事情.我认为最好的办法是在显示ColorBox时将事件处理程序绑定到关闭按钮:

onComplete:function(){
  $("#cboxClose").click(function(e) {
    // stop any other script from firing
    e.stopPropagation(); 
    if (confirm('Are you sure')) {
      $.colorbox.close();
      // ensure that the binding is removed when closed
      $("#cboxClose").unbind();
    }
  });
}
Run Code Online (Sandbox Code Playgroud)


Tom*_*mmy 5

@Ken,你的例子对我来说非常合适......差不多.我必须做的唯一修改是在设置click函数之前取消绑定,因为由于某种原因,它会忽略我的if语句并且在第一次加载时仍然关闭.以下是我用于确认关闭彩盒的内容

$(".Add").colorbox({
    onComplete:function(e){
       $("#modalForm").ajaxForm(modalOptions);
       $("#cboxClose").unbind(); 
       $("#cboxClose").click(function(e){
          e.stopPropagation();
          if(confirm('Are you sure you want to cancel your changes?')){
             $.colorbox.close();
             $("#cboxClose").unbind();                               
          }                           
       }); 
    }
   });
Run Code Online (Sandbox Code Playgroud)