我使用alert()
将我的验证错误输出回给用户,因为我的设计没有提供任何其他东西,但我更愿意使用jQuery UI对话框作为我的消息的警告对话框。
因为错误不包含在(html) div中,所以我不确定该怎么做。通常,您会将dialog()
分配给一个div,比如$("#divName").dialog()
,但我更需要一个js函数,比如alert_dialog("Custom message here")
或类似的函数。
有什么想法吗?
发布于 2011-11-10 20:46:18
我认为您甚至不需要将其附加到DOM,这似乎对我有效:
$("<div>Test message</div>").dialog();
这是一个JS小提琴:
发布于 2012-10-28 17:15:19
使用这里的一些信息,我最终创建了自己的函数来使用。
可以用作..。
custom_alert();
custom_alert( 'Display Message' );
custom_alert( 'Display Message', 'Set Title' );
jQuery UI警报替换
function custom_alert( message, title ) {
if ( !title )
title = 'Alert';
if ( !message )
message = 'No Message to Display.';
$('<div></div>').html( message ).dialog({
title: title,
resizable: false,
modal: true,
buttons: {
'Ok': function() {
$( this ).dialog( 'close' );
}
}
});
}
发布于 2015-11-06 10:20:04
基于eidylon的答案,这里有一个版本,如果TitleMsg为空,则不会显示标题栏:
function jqAlert(outputMsg, titleMsg, onCloseCallback) {
if (!outputMsg) return;
var div=$('<div></div>');
div.html(outputMsg).dialog({
title: titleMsg,
resizable: false,
modal: true,
buttons: {
"OK": function () {
$(this).dialog("close");
}
},
close: onCloseCallback
});
if (!titleMsg) div.siblings('.ui-dialog-titlebar').hide();
}
请参阅jsfiddle
https://stackoverflow.com/questions/8079674
复制相似问题