Javascript 事件上的jQuery插件返回True/False
我正在为一个项目创建一个插件来代替提醒/确认,我很好奇是否有一种方法可以让它像一个真正的确认,你可以做到:Javascript 事件上的jQuery插件返回True/False,javascript,jquery,events,jquery-plugins,boolean,Javascript,Jquery,Events,Jquery Plugins,Boolean,我正在为一个项目创建一个插件来代替提醒/确认,我很好奇是否有一种方法可以让它像一个真正的确认,你可以做到: 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*/} 以下是我到目前为止在
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*/}
以下是我到目前为止在点击事件中找到的所有CAPS注释(请记住,这仍在开发中,因此HTML和其他内容仍然有点讨厌):
(函数($){
$.alert=功能(消息、选项){
默认值={
类型:'alert',
回调:函数(){}
}
选项=$.extend({},默认值,选项);
如果(options.type=='confirm'){
$(“”+message+”
).prependTo('body');
}
否则{
$(“”+message+”
).prependTo('body');
}
$alertboxclass=$('.customalertbox');
$alerthiddenoverlay=$('.alerthiddenoverlay');
$alertboxclass.find('a')。单击(函数(事件){
event.preventDefault();
var_返回=false;
if($(this.attr('href')=='#ok'){
var_return=true;
}
$alertboxclass.fadeOut(250,函数(){$alerthiddenoverlay.delay(250).fadeOut(250,函数(){$(this.remove();选项.callback.call(this,the_return);});$(this.remove());
});
$alertboxclass.css({
顶部:$(窗口).height()/2-$alertboxclass.height()/2,
左:$(窗口).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);
我看到一个不错的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;
});
});
我认为将回调方法作为参数传递给
$.alert
函数将是最简单的选择。如果这是一个交易破坏者,我可以看看链接事件的.queue()
方法
你能把这个放到jsbin.com上,让我们看到它的作用,并做出新的修改与你分享吗?
/* 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;
});
});