我知道我可以致电alert('Warning1');alert('Warning2');
它将显示2个警报。但是,当我使用JAlert Page中提到的JAlert插件时,我无法显示多条警报消息。你们有没有人使用过这个插件并解决了同样的问题?
答案 0 :(得分:4)
是的 - 所以我制作了一个示例HTML并测试了这个东西
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<!-- Dependencies -->
<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery-ui.min.js" type="text/javascript"></script>
<!-- Core files -->
<script src="jquery.alerts.js" type="text/javascript"></script>
<link href="jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript">
$(document).ready( function() {
jAlert('This is a custom alert box', 'Alert Dialog', doAlert() );
function doAlert() {
alert('CallBack')
}
});
</script>
</head>
<body>
</body>
所以基于网站的文档
用法 这个插件使用$ .alerts命名空间,但有三个内置的快捷方式功能&gt;使实现更容易:
jAlert(消息,[标题,回调])
好的,现在这是jQuery的逻辑
实际发生的事情
总结
此插件无法在内部处理多个调用,并且回调错误!因为它没有回调,而是在调用函数之前调用函数或等待接受初始jAlert
解决方案
为什么警报();然后工作??!?!?!?!?
因为当您调用alert();
代码执行STOPS并等待,直到您按OK并继续代码。
所以我很遗憾地说,但这个插件运行不正常,我建议你找另一个。
答案 1 :(得分:1)
我遇到了同样的问题,并解决了这个问题: 在jquery.alerts.js中,就在公共函数的注释之前,我插入了这个:
raiseNextDialog: function() {
$.alerts.callIsActive = false;
if ($.alerts.waitingCalls.length>0) {
var params = $.alerts.waitingCalls.shift();
$.alerts._show(params[0], params[1], params[2], params[3], params[4]);
}
},
dialogShallWait: function (title, msg, value, type, callback) {
if ($.alerts.callIsActive) {
$.alerts.waitingCalls.push([title, msg, value, type, callback]);
return true; // can't show now
} else {
$.alerts.callIsActive = true;
return false;
}
},
我们的想法是,如果之前的对话框仍处于打开状态,我们会将调用推送到数组中,以便在对话框关闭时使用。
现在我们仍然需要在_show()函数的开头添加一行:
if ( $.alerts.dialogShallWait(title, msg, value, type, callback) ) return;
对于五个.click()处理程序,我们需要在调用回调后添加此行:
$.alerts.raiseNextDialog();
这就是全部!
我想更新创作者但是没有办法在他的网站上发表评论...... 我正在使用演示here的精彩版本,它使用标准主题。太棒了!
答案 2 :(得分:1)
之前的答案部分正确,但无法定义包含多个警报的数组($ .alerts.waitingCalls)。下面的代码替换了整个jquery.alerts.js(整个包可在以下位置获取:http://code.google.com/p/jalert-plus/downloads/list)
// jQuery Alert Dialogs Plugin
//
// Version 1.1 (extended)
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 14 May 2009
//
// Mike Walters
// 27 December 2011
// http://code.google.com/p/jalert-plus/downloads/list
//
// Visit http://abeautifulsite.net/notebook/87 for more information
//
// Usage:
// jAlert( message, [title, callback] )
// jConfirm( message, [title, callback] )
// jPrompt( message, [value, title, callback] )
//
// History:
//
// 1.00 - Released (29 December 2008)
//
// 1.01 - Fixed bug where unbinding would destroy all resize events
//
// DECEMBER 2011 - added ability to popup multiple alerts (mike walters)
//
// License:
//
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC.
//
(function($) {
$.alerts = {
// These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time
verticalOffset: -75, // vertical offset of the dialog from center screen, in pixels
horizontalOffset: 0, // horizontal offset of the dialog from center screen, in pixels/
repositionOnResize: true, // re-centers the dialog on window resize
overlayOpacity: .01, // transparency level of overlay
overlayColor: '#FFF', // base color of overlay
draggable: true, // make the dialogs draggable (requires UI Draggables plugin)
okButton: ' OK ', // text for the OK button
cancelButton: ' Cancel ', // text for the Cancel button
dialogClass: null, // if specified, this class will be applied to all dialogs
waitingCalls: new Array(),
raiseNextDialog: function() {
$.alerts.callIsActive = false;
if ($.alerts.waitingCalls.length>0) {
var params = $.alerts.waitingCalls.shift();
$.alerts._show(params[0], params[1], params[2], params[3], params[4]);
}
},
dialogShallWait: function (title, msg, value, type, callback) {
if ($.alerts.callIsActive) {
$.alerts.waitingCalls.push([title, msg, value, type, callback]);
return true; // can't show now
} else {
$.alerts.callIsActive = true;
return false;
}
},
// Public methods
alert: function(message, title, callback) {
if( title == null ) title = 'Alert';
$.alerts._show(title, message, null, 'alert', function(result) {
if( callback ) callback(result);
});
},
confirm: function(message, title, callback) {
if( title == null ) title = 'Confirm';
$.alerts._show(title, message, null, 'confirm', function(result) {
if( callback ) callback(result);
});
},
prompt: function(message, value, title, callback) {
if( title == null ) title = 'Prompt';
$.alerts._show(title, message, value, 'prompt', function(result) {
if( callback ) callback(result);
});
},
// Private methods
_show: function(title, msg, value, type, callback) {
if ( $.alerts.dialogShallWait(title, msg, value, type, callback) ) return;
$.alerts._hide();
$.alerts._overlay('show');
$("BODY").append(
'<div id="popup_container">' +
'<h1 id="popup_title"></h1>' +
'<div id="popup_content">' +
'<div id="popup_message"></div>' +
'</div>' +
'</div>');
if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass);
// IE6 Fix
var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';
$("#popup_container").css({
position: pos,
zIndex: 99999,
padding: 0,
margin: 0
});
$("#popup_title").text(title);
$("#popup_content").addClass(type);
$("#popup_message").text(msg);
$("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );
$("#popup_container").css({
minWidth: $("#popup_container").outerWidth(),
maxWidth: $("#popup_container").outerWidth()
});
$.alerts._reposition();
$.alerts._maintainPosition(true);
switch( type ) {
case 'alert':
$("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /></div>');
$("#popup_ok").click( function() {
$.alerts._hide();
callback(true);
$.alerts.raiseNextDialog();
});
$("#popup_ok").focus().keypress( function(e) {
if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click');
});
break;
case 'confirm':
$("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
$("#popup_ok").click( function() {
$.alerts._hide();
if( callback ) callback(true);
$.alerts.raiseNextDialog();
});
$("#popup_cancel").click( function() {
$.alerts._hide();
if( callback ) callback(false);
$.alerts.raiseNextDialog();
});
$("#popup_ok").focus();
$("#popup_ok, #popup_cancel").keypress( function(e) {
if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
});
break;
case 'prompt':
$("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
$("#popup_prompt").width( $("#popup_message").width() );
$("#popup_ok").click( function() {
var val = $("#popup_prompt").val();
$.alerts._hide();
if( callback ) callback( val );
$.alerts.raiseNextDialog();
});
$("#popup_cancel").click( function() {
$.alerts._hide();
if( callback ) callback( null );
$.alerts.raiseNextDialog();
});
$("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) {
if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
});
if( value ) $("#popup_prompt").val(value);
$("#popup_prompt").focus().select();
break;
}
// Make draggable
if( $.alerts.draggable ) {
try {
$("#popup_container").draggable({ handle: $("#popup_title") });
$("#popup_title").css({ cursor: 'move' });
} catch(e) { /* requires jQuery UI draggables */ }
}
},
_hide: function() {
$("#popup_container").remove();
$.alerts._overlay('hide');
$.alerts._maintainPosition(false);
},
_overlay: function(status) {
switch( status ) {
case 'show':
$.alerts._overlay('hide');
$("BODY").append('<div id="popup_overlay"></div>');
$("#popup_overlay").css({
position: 'absolute',
zIndex: 99998,
top: '0px',
left: '0px',
width: '100%',
height: $(document).height(),
background: $.alerts.overlayColor,
opacity: $.alerts.overlayOpacity
});
break;
case 'hide':
$("#popup_overlay").remove();
break;
}
},
_reposition: function() {
var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
if( top < 0 ) top = 0;
if( left < 0 ) left = 0;
// IE6 fix
if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
$("#popup_container").css({
top: top + 'px',
left: left + 'px'
});
$("#popup_overlay").height( $(document).height() );
},
_maintainPosition: function(status) {
if( $.alerts.repositionOnResize ) {
switch(status) {
case true:
$(window).bind('resize', $.alerts._reposition);
break;
case false:
$(window).unbind('resize', $.alerts._reposition);
break;
}
}
}
}
// Shortuct functions
jAlert = function(message, title, callback) {
$.alerts.alert(message, title, callback);
}
jConfirm = function(message, title, callback) {
$.alerts.confirm(message, title, callback);
};
jPrompt = function(message, value, title, callback) {
$.alerts.prompt(message, value, title, callback);
};
})(jQuery);