gpt4 book ai didi

jquery - 使用jqueryUI 对话框模仿confirm()

转载 作者:行者123 更新时间:2023-12-01 01:03:19 24 4
gpt4 key购买 nike

我想使用 jQueryUI 对话框来模拟标准 JavaScript 确认()。我正在考虑类似以下的事情,但我显然不明白它应该如何工作。有什么建议么?谢谢

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;}}
});

最佳答案

重复的确实没什么用。对此我深感抱歉。

基于此answer ,这就是我要做的:

  • 创建一个函数,该函数将创建一个带有消息和“确定”/“取消”按钮的基本模式对话框

  • 接受单击两个按钮时执行的两个回调

好处是它不会像答案中那样用无限循环阻塞整个浏览器。 jQuery UI 对话框的选项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");
}
}
});

}

你可以这样使用它:

$('button').click(function(e) {

var okHandler = function() {
alert('ok');
};

confirmDialog('Do you really want ?', okHandler);
});

<强> DEMO

关于jquery - 使用jqueryUI 对话框模仿confirm(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8913561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com