jquery - 在被调用之前执行的回调

标签 jquery html callback

我正在尝试使用花式框实现确认对话框。我的 javascript 代码被安排到模块中。

我在名为“Utility”的模块中设置了确认对话框功能

var Utility = (function() {
    function confirm_dialog(title,msg,callback)
        {
            var html = '<div class="confirm-box">'+
                       '<div class="confirm-title">'+title+'</div>'+
                       '<div class="confirm-msg">'+msg+'</div>'+
                       '<div class="confirm-buttons">'+
                       '<button class="okBtn">Ok</button>'+
                       '<button class="cancelBtn">Cancel</button>'+
                       '</div>'+
                       '</div>';
            var ret;
            $.fancybox('<div class="confirm-dialog"></div>',
            {
                'width':400,
                'height':250,
                'content':html,
                'modal' : true,
                'transitionIn' : 'none',
                'transitionOut': 'none',
                'speedIn' : 300,
                'speedOut' : 300,
                'autoScale' : false,
                'scrolling' : 'no',
                'overlayShow' : true,
                'overlayOpacity' : 0.3,
                'overlayColor' : '#666',
                'padding':10,
                'onComplete':function() {
                    $('.okBtn').click(function(){
                            ret = true;
                            $.fancybox.close();
                        })
                    $('.cancelBtn').click(function() {
                        ret = false;
                        $.fancybox.close();
                    })
                },
                onClosed : function() {
                     if (typeof callback == 'function'){ callback.call(this, ret); } }
            });
        }
        return { dialog : confirm_dialog }
})();

这是触发对话框的事件处理程序

$('a.deletePheed').click(function(e) {
                            var id = $(this).parent('div.pheed-holder').attr('data-id');
                        Utility.dialog('Delete Pheed','Are sure you want to delete this pheed ?',
                        PheedModule.delete_pheed());
                            e.preventDefault();
                        });

一旦事件处理程序被点击,回调将在对话框显示之前立即触发,那些在确认对话框中点击确定时不执行的事件。 我做错了什么?

最佳答案

我不熟悉您正在使用的所有内容,但您可能需要将对 PheedModule.delete_pheed() 的调用包装在一个匿名函数中:

Utility.dialog('Delete Pheed','Are sure you want to delete this pheed ?', function() {
    PheedModule.delete_pheed();
});

关于jquery - 在被调用之前执行的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8095213/

相关文章:

javascript - 通过 javascript 确定更改了哪些字段

javascript - jQuery 事件处理程序不会在单击时激活

javascript - 我可以使用什么语言/脚本将 .M4V 文件嵌入到网页中?

html - Sightly:迭代一个列表并使用其索引打印另一个列表(不嵌套,只是共享索引)

android - 什么是回调以及如何在 Android 中实现它们?

javascript - 此 jQuery getJSON AJAX 调用的回调方法存在问题

ruby-on-rails - 将参数传递给 Controller ​​中的回调

使用 jQuery UI 时,Jquery Slimscroll 在 IE-9 和 IE-10 中不可拖动

javascript - Internet Explorer 中的元素在滚动时跳跃

html - 用户可调整最大宽度/高度的 Div