jQuery插件的事件返回True/False

3
我正在创建一个插件,用于替换项目中的警报/确认,我想知道是否有办法让它像真正的确认一样,你可以这样做:
if(confirm('Yes or no?')){alert('You agreed!');}

现在我需要使用以下语法进行回调:

$.alert('yes or no',{type:'confirm'});

但我想要做到以下几点:

if($.alert('yes or no',{type:'confirm'})){/*some action on true*/}

这是我目前的进展,注意点击事件中所有大写字母的注释(记住,这仍在开发中,因此HTML和其他内容仍然有些混乱):

(function($) {
    $.alert = function(message,options) {
        defaults = {
            type:'alert',
            callback:function(){}
        }
        options = $.extend({},defaults,options);
        if(options.type == 'confirm'){
            $('<div style="display:none" class="alerthiddenoverlay"></div><div style="display:none" class="customalertbox"><div><img src="http://cdn.iconfinder.net/data/icons/basicset/tick_48.png"><p>'+message+'</p><br class="clear"><span><a class="cancel" href="#cancel">Cancel</a><a class="ok" href="#ok">OK</a></span><br class="clear"></div></div>').prependTo('body');
        }
        else{
            $('<div style="display:none" class="alerthiddenoverlay"></div><div style="display:none" class="customalertbox"><div><img src="http://cdn.iconfinder.net/data/icons/basicset/warning_48.png"><p>'+message+'</p><br class="clear"><span><a class="ok" href="#ok">OK</a></span><br class="clear"></div></div>').prependTo('body');
        }
        $alertboxclass=$('.customalertbox');
        $alerthiddenoverlay=$('.alerthiddenoverlay');
        $alertboxclass.find('a').click(function(event){
            event.preventDefault();
            var the_return = false;
            if($(this).attr('href') == '#ok'){
                var the_return = true;
            }
            $alertboxclass.fadeOut(250,function(){$alerthiddenoverlay.delay(250).fadeOut(250,function(){$(this).remove();options.callback.call(this,the_return);});$(this).remove()});
        });
        $alertboxclass.css({
            top:$(window).height()/2-$alertboxclass.height()/2,
            left:$(window).width()/2-$alertboxclass.width()/2
        });
        $alerthiddenoverlay.css({height:$(window).height()+'px',width:'100%',position:'fixed',zIndex:'9998'}).fadeIn(250,function(){$alertboxclass.delay(250).fadeIn()});
    }
})(jQuery);

你可以将这个放在jsbin.com上,这样我们就可以看到它的运行情况,并且制作新的版本与你分享吗? - Bob Gregor
2个回答

1

我认为将回调方法作为参数传递给$.alert函数将是最简单的选择。如果这是一个让人无法接受的问题,我可能会考虑使用.queue()方法来链接事件。

http://api.jquery.com/queue/


0

我看到了一个很好的confirm()覆盖到模态窗口的例子,作为jqModal的示例

这是代码示例。我相信你可以根据需要进行调整...

/* Overriding Javascript's Confirm Dialog */

// NOTE; A callback must be passed. It is executed on "cotinue". 
//  This differs from the standard confirm() function, which returns
//   only true or false!

// If the callback is a string, it will be considered a "URL", and
//  followed.

// If the callback is a function, it will be executed.


function confirm(msg,callback) {
  $('#confirm')
    .jqmShow()
    .find('p.jqmConfirmMsg')
      .html(msg)
    .end()
    .find(':submit:visible')
      .click(function(){
        if(this.value == 'yes')
          (typeof callback == 'string') ?
            window.location.href = callback :
            callback();
        $('#confirm').jqmHide();
      });
}


$().ready(function() {
  $('#confirm').jqm({overlay: 88, modal: true, trigger: false});

  // trigger a confirm whenever links of class alert are pressed.
  $('a.confirm').click(function() { 
    confirm('About to visit: '+this.href+' !',this.href); 
    return false;
  });
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接