Fancybox的Javascript/Jquery回调是否确认 [英] Javascript/Jquery Callback for Fancybox Yes No Confirm

查看:69
本文介绍了Fancybox的Javascript/Jquery回调是否确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现幻想是否确认,并且在使回调根据需要工作时遇到问题.下面的示例使用 Fancybox v2 .

I'm trying to implement a fancy Yes No confirm and I'm having problems getting the callback to work as needed. The example below is using Fancybox v2.

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

The problem with the code below is that the function "do_something" is being call when HREF "Test" is clicked and not being called when the user clicks #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>

推荐答案

只要用户单击链接,就会执行您的方法"do_something('a','b')",并且返回的结果将作为第二个传递"fancyConfirm"方法的参数.这就是为什么您的参数回调"始终为未定义"的原因.

Your method "do_something('a', 'b')" will be executed whenever user clicks on the link and the returned result will be passed as the second parameter to the "fancyConfirm" method. This is why your parameter "callback" is always "undefined".

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

There is also a problem with fancyBox - "afterClose" callback gets called twice - before opening and after closing (this will be changed in the next release).

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

I would bind a click event on the link and call a callback when user clicks on one of the buttons, like - http://jsfiddle.net/xcJFY/1/

这篇关于Fancybox的Javascript/Jquery回调是否确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆