我正在使用 jquery-ui dialog box
我的问题是在单击 x 按钮关闭对话框时,我还需要执行 cancel() 函数。

我怎样才能做到这一点?

var content =
{
    autoOpen    : false,
    modal       : true,
    width       : 350,
    minHeight   : 50,
    height      : 350,
    position    : "center",
    resizable   : false,
    draggable   : false,
    close       : function () {$(".privacy_modal").prop("checked", false);},
    buttons:
    {
        "Cancel": function cancel()
        {
            $(".privacy_modal").prop("checked", false); $(this).dialog("close");
        },
        "Accept": function accept()
        {
            $(".privacy_modal").prop("checked", true); $(this).dialog("close");
        }
    }
};

注意:使用 close 并不能解决我的问题,因为当我单击接受按钮时它会覆盖该功能

最佳答案

您可以使用第三方变量(默认情况下为 False 的 bAccepts)和第三方方法。

当用户接受时:

  • 将 bAccepts 设置为 True

  • 当用户取消时:
  • 将 bAccepts 设置为 False

  • 当 onClose 被触发时,调用 doClose() 方法,它执行以下操作:
  • 如果 bAccepts 为 True => 接受
  • else => 取消

  • 这是一些未经测试的伪代码。见 working code
    var bAccepts = false;
    var content = {
                        autoOpen    : false,
                        modal       : true,
                        width       : 350,
                        minHeight   : 50,
                        height      : 350,
                        position    : "center",
                        resizable   : false,
                        draggable   : false,
                        close       : function () { if (bAccepts) {...} else {...} },
                        buttons: {
                            "Cancel": function cancel() { bAccepts = false; $(this).dialog("close");},
                            "Accept": function accept() { bAccepts = true; $(this).dialog("close");}
                 }
    };
    

    关于JQUERY-ui对话框x按钮功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11240025/

    10-12 06:51