我正在寻找一种方法来使用JQuery实现可重用的“确认”对话..
这是MyApp类打开对话框的部分:
/**
* @param text string Message to display
*/
getConfirmationDialog: function(text) {
MyApp.confirmDialog = $('<div><p>' + text + '</p></div>');
MyApp.confirmDialog
.dialog({
modal: true,
autoOpen: false,
title: 'Please confirm',
width: 300,
height: 180,
buttons: {
'OK': function() {
return true;
},
Cancel: function() {
$(this).dialog('close');
return false;
}
}
});
MyApp.confirmDialog.dialog('open');
},
在另一堂课中我做了:
/**
* Clear system cache
*
* @param url string Backend URL
*/
clearCache: function(url) {
dialog = MyApp.getConfirmationDialog('Clear cache?');
//dialog returns true..
if (dialog) {
MyApp.admin.dashboard.doClearCache();
}
},
但我不知道以“正确”的方式做到这一点..对话框无法返回值或?
答案 0 :(得分:5)
以下代码是我解决此问题的方法。我记录了函数中的用法,但在此强调它:
$.ConfirmDialog('Do you want to continue?', 'Continue Title', function() { alert('yes'); }, function() { alert('no'); }, null);
无需特殊设置,只需在页面上包含“ConfirmDialog”代码块(我将其放入我的app.js中)并使用上面的单行调用。请享用!
$.ConfirmDialog = function (message, title, callbackYes, callbackNo, callbackArgument) {
/// <summary>
/// Generic confirmation dialog.
///
/// Usage:
/// $.ConfirmDialog('Do you want to continue?', 'Continue Title', function() { alert('yes'); }, function() { alert('no'); }, null);
/// $.ConfirmDialog('Do you want to continue?', 'Continue Title', function(arg) { alert('yes, ' + arg); }, function(arg) { alert('no, ' + arg); }, 'please');
///</summary>
///<param name="message" type="String">
/// The string message to display in the dialog.
///</param>
///<param name="title" type="String">
/// The string title to display in the top bar of the dialog.
///</param>
///<param name="callbackYes" type="Function">
/// The callback function when response is yes.
///</param>
///<param name="callbackNo" type="Function">
/// The callback function when response is no.
///</param>
///<param name="callbackNo" type="Object">
/// Optional parameter that is passed to either callback function.
///</param>
if ($("#modalConfirmDialog").length == 0)
$('body').append('<div id="modalConfirmDialog"></div>');
var dlg = $("#modalConfirmDialog")
.html(message)
.dialog({
autoOpen: false, // set this to false so we can manually open it
dialogClass: "confirmScreenWindow",
closeOnEscape: true,
draggable: false,
width: 460,
minHeight: 50,
modal: true,
resizable: false,
title: title,
buttons: {
Yes: function () {
if (callbackYes && typeof (callbackYes) === "function") {
if (callbackArgument == null) {
callbackYes();
} else {
callbackYes(callbackArgument);
}
}
$(this).dialog("close");
},
No: function () {
if (callbackNo && typeof (callbackNo) === "function") {
if (callbackArgument == null) {
callbackNo();
} else {
callbackNo(callbackArgument);
}
}
$(this).dialog("close");
}
}
});
dlg.dialog("open");
};
答案 1 :(得分:4)
有关确认按钮,请参阅上面的vinay答案。我重复使用它来创建一个简单但可重复使用的对话框,其中有一个ok按钮用于正常目的,ok n取消确认。您也可以动态设置自定义标题和内容。
<div id="yeah" title="Alert">
<p id="yeah_msg"> </p>
</div>
<button id="click_me">Show it</button>
<script type="text/javascript">
function dlg(msg, callback, title){
if(callback == undefined){
callback = null;
}
if(title == undefined){
title = 'Alert';
}
$("#yeah_msg").html(msg);
$("#yeah").dialog('option', 'title', title);
if(callback){
$("#yeah").dialog('option', 'buttons', {
"Ok": function() {
$( this ).dialog( "close" );
if(null != callback) callback.success();
},
'Cancel': function(){
$( this ).dialog( "close" );
if(null != callback) callback.fail();
}
});
}else{
$("#yeah").dialog('option', 'buttons', {
"Ok": function() {
$( this ).dialog( "close" );
}
});
}
$("#yeah").dialog("open");
}
$(document).ready(function(){
//create dialog
$("#yeah").dialog({
autoOpen: false,
modal: true,
show: 'blind',
hide: 'explode',
resizable: false
});
//show dialog
$('#click_me').click(function(){
dlg('title', {success: function(){ console.log('yipee'); }, fail: function(){ console.log('nopee'); } });
});
});
</script>
答案 2 :(得分:3)
jQuery ui没有提供更改对话框按钮事件的好方法。
我会使用pubsub模式,来自Cowboyba的小pubsub插件here或phiggins努力here。它比尝试使用jquery ui getters和setter尝试更改按钮及其点击事件更清晰,如果你制作一个大型应用程序将在很多其他地方帮助你。
您可以发布事件以单击“确定”按钮,然后订阅并取消订阅其他处理程序以收听该事件。
Quick Demo此处显示功能。
答案 3 :(得分:3)
创建确认类。
//下面是确认类骨架
function ConfirmDialog() {
this.showMessage = function(message, callback, argument) {
var $dialog = $('<div></div>')
.html(message)
.dialog({
modal: true,
closeOnEscape: true,
buttons: {
Yes: function() {
if (callback && typeof(callback) === "function") {
if (argument == 'undefined') {
callback();
} else {
callback(argument);
}
}
$(this).dialog("close");
},
No: function() {
$(this).dialog("close");
}
}
});
$dialog.dialog("open");
}
}
创建confirmDialog类型的对象并放入.jsp
CONFIRM_DIALOG = new ConfirmDialog();
使用一个参数创建回调消息。
function saved(what) {
alert("Saved: " + what);
}
在需要的地方拨打电话。
CONFIRM_DIALOG.showMessage("Do you wish to marry?", saved, "Life");
工作完成!!
答案 4 :(得分:2)
我想我得到你所说的。看看我的jsfiddle attempt,看看它是否对你有帮助。我认为它正在做你正在尝试的事情。
答案 5 :(得分:1)
我已成功在Jquery中实现了确认框。在尝试此操作之前,请确保您的代码中包含Jquery库和css INCLUDED(jquery-ui-1.8.16.custom.css,jquery-1.6.2.js,jquery-ui-1.8.16.custom.min。 JS)。 我们创建使用DIV的JAVASCRIPT CONFIRM BOX和这个盒子之间的主要区别 - 是 - JAVASCRIPT CONFIRM将等待用户输入,在用户输入之后是/否下一行将执行,这里你必须做,是或否BLOCK - ** showConfirm()将立即执行后的下一行代码 * 所以要小心
/** add this div to your html
* /
var $confirm;
var callBack;
var iconStyle = '<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 50px 0;"></span>';
var messageStyleStart = '<span align="center" style="font-family:arial, verdana, sans-serif;font-size:9.3pt;">';
var messageStyleEnd = '</span>';
$(document).ready(function(){
$('#confirmDialog').dialog({
autoOpen: false,
modal: true
});
//jquery confirm box -- the general alert box
$confirm = $('<div style="vertical-align:middle;"></div>')
.html('This Message will be replaced!')
.dialog({
autoOpen: false,
modal: true,
position: 'top',
height:300,
width: 460,
modal: true,
buttons: {
Ok : function() {
$( this ).dialog( "close" );
if(null != callBack)
callBack.success();
},
Cancel: function() {
$( this ).dialog( "close" );
if(null != callBack)
callBack.fail();
}
}
});
});
function showConfirm(message,callBackMe,title){
callBack = null;
$confirm.html(""); // work around
$confirm.html(iconStyle + messageStyleStart +message + messageStyleEnd);
if(title =='undefined'|| null ==title)
$confirm.dialog( "option", "title", "Please confirm" );
else
$confirm.dialog( "option", "title", title);
var val = $confirm.dialog('open');
callBack = callBackMe;
// prevent the default action
return true;
}
// Now for calling the function
// create a Javascript/jSOn callback object
var callMeBack = {
success: function()
{ // call your yes function here
clickedYes();
return;
},
fail: function (){
// call your 'no' function here
clickedNo();
return ;
}
};
showConfirm("Do you want to Exit ?<br/>"+
,callMeBack1,"Please Answer");
答案 6 :(得分:1)
哇,为什么这么复杂?这是一个简单的方法
$("#myButton").click(function(event) {
var cont = confirm('Continue?');
if(cont) {
// do stuff here if OK was clicked
return true;
}
// If cancel was clicked button execution will be halted.
event.preventDefault();
}