Fancybox的Javascript/Jquery回调是否确认

Pau*_*bow 3 javascript jquery fancybox

我正在尝试实现一种幻想是没有确认,我遇到问题让回调按需工作.以下示例使用Fancybox v2.

以下代码的问题是,当HREF单击"测试"并且在用户单击时未调用时,正在调用函数"do_something" #fancyConfirm_ok.

function fancyConfirm(msg,callback) {
    var ret;
    jQuery.fancybox({
        'modal' : true,
        'content' : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input id=\"fancyconfirm_cancel\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Cancel\"><input id=\"fancyConfirm_ok\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Ok\"></div></div>",
        'afterShow' : function() {
            jQuery("#fancyconfirm_cancel").click(function() {
                ret = false;
                //jQuery.fancybox.close();
                $.fancybox.close();
            })
            jQuery("#fancyConfirm_ok").click(function() {
                ret = true;
                jQuery.fancybox.close();
            })
        },
        'afterClose' : function() { 
            if (typeof callback == 'function'){ 
                callback.call(this, ret); 
            } else {
                var callback_type = typeof callback;
                alert(callback_type);
            }
        }
    });
}
<a href="#" onclick="fancyConfirm('Are you sure you want to delete?',  do_something('a', 'b') );return false;">Test</a>
Run Code Online (Sandbox Code Playgroud)

Jan*_*nis 11

每当用户点击链接时,您的方法"do_something('a','b')"将被执行,返回的结果将作为第二个参数传递给"fancyConfirm"方法.这就是您的参数"回调"始终是"未定义"的原因.

fancyBox也存在问题 - "afterClose"回调被调用两次 - 在打开之前和关闭之后(这将在下一个版本中更改).

我会在链接上绑定一个click事件,并在用户点击其中一个按钮时调用回调,例如 - http://jsfiddle.net/xcJFY/1/