3

如果用户尝试关闭颜色框,我想显示简单的确认弹出框。我试过这段代码:

onCleanup:function(){ confirm('Are you sure'); }

它显示确认框,但即使我单击“取消”,颜色框也会关闭!

有人可以帮忙吗?

4

4 回答 4

9

colorbox的FAQ中已经有一个很好的例子

您必须重新定义 colorbox 的关闭方法:

var originalClose = $.colorbox.close;
$.colorbox.close = function(){
  if (confirm('Do you want to close this window?')) {
    originalClose();
  }
};
于 2012-04-16T10:25:09.343 回答
5

I've done something similar with FancyBox. I think your best bet is to bind an event handler to the close button when the ColorBox is displayed:

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();
    }
  });
}
于 2010-07-10T14:33:50.840 回答
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();                               
          }                           
       }); 
    }
   });
于 2010-08-18T17:45:51.623 回答
0

我不知道 colorbox 是否允许在您启动后取消关闭..

但是,如果确实如此,您需要将代码更改为

onCleanup:function(){ return confirm('Are you sure'); }
于 2010-07-10T13:50:06.903 回答