使用jqueryUI对话框模拟confirm()
我想使用jQueryUI对话框模拟标准JavaScript confirm()。我在想下面这样的事情,但我显然不明白它应该如何工作。有什么建议吗?多谢各位使用jqueryUI对话框模拟confirm(),jquery,jquery-ui-dialog,Jquery,Jquery Ui Dialog,我想使用jQueryUI对话框模拟标准JavaScript confirm()。我在想下面这样的事情,但我显然不明白它应该如何工作。有什么建议吗?多谢各位 return $("#dialog-cancel").dialog("open"); $("#dialog-cancel").dialog({ autoOpen: false,height: 400,width: 350,modal: true, open: function(event, ui){}, button
return $("#dialog-cancel").dialog("open");
$("#dialog-cancel").dialog({
autoOpen: false,height: 400,width: 350,modal: true,
open: function(event, ui){},
buttons: {'OK': function(){$(this).dialog("close");return true;},'CANCEL': function() {$(this).dialog("close");return false;}}
});
复制品确实没有什么用处。对此我很抱歉 基于此,我将这样做:
- 创建一个函数,该函数将创建一个带有消息和“确定/取消”按钮的基本模式对话框
- 接受两个按钮在单击时执行的回调
modal
选项只会阻塞当前页面
代码如下:
function confirmDialog(message, onOK, onCancel) {
$('<div>' + message + '</div>').dialog({
modal: true,
buttons : {
"OK" : function() {
$(this).dialog("close");
// if there is a callback, execute it
if (onOK && $.isFunction(onOK)) {
onOK();
}
// destroy the confirmation dialog
$(this).dialog("destroy");
},
"Cancel" : function() {
$(this).dialog("close");
if (onCancel && $.isFunction(onCancel)) {
onCancel();
}
$(this).dialog("destroy");
}
}
});
}
可能的重复事实上,也许我还是不明白。如何将对话框的响应返回到原始调用脚本?您无法实现它,使其与
confirm
方法完全相同。您必须定义当用户单击其中一个按钮时要运行的方法。你不能在if声明中使用它。谢谢Kevin,你所描述的就是我过去是如何做的。对于这一次,我希望由两个不同的操作触发一个外观相同的对话框,但随后对每个操作执行不同的确认方法。对于所有的新手问题,我感到抱歉,但回调函数对我来说相当陌生。如果我想给onOk发送一些东西,比如$('button'),怎么样。我试过…$t=$(这个);var okHandler=函数($t){…但它不起作用。如何解释。你可以先阅读和。听起来很乏味,但回调是javascript的重要组成部分。问题是,你不能像你想为okHandler
做的那样有参数,但是你可以在函数中使用$t
变量,而不传递参数,因为范围仍然存在。我有更新d JSFIDLE供您查看。检查$span
变量。哈哈,在收到您的回复之前,我已经准备好了您的第一篇建议文章。我阅读了第二篇,并对其进行了进一步思考。谢谢!
$('button').click(function(e) {
var okHandler = function() {
alert('ok');
};
confirmDialog('Do you really want ?', okHandler);
});