JQUERY-ui对话框x按钮功能

时间:2012-06-28 07:33:20

标签: jquery jquery-ui jquery-ui-dialog

我正在使用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"); 
        }
    }
};

TEST

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

2 个答案:

答案 0 :(得分:7)

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

当用户接受:

  • 将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");}
             }
};

答案 1 :(得分:3)

工作演示 http://jsfiddle.net/Ea6Hm/1/

您可以使用:http://docs.jquery.com/UI/Dialog#event-beforeClose

使用beforeClose,您可以在关闭对话框之前调用要调用的任何函数。

希望这有帮助,

<强>码

$(document).ready(function() {
    $('#theLink').click(function() {
        $("#forgot-dialog").dialog("open");
    });

    $("#forgot-dialog").dialog({
        modal: true,
        autoOpen: false,
        height: 255,
        width: 300,
        beforeClose: function() {
            alert("Do whatever before Close");
        },
        buttons: {
            "Retrieve": function() {
                document.forms["forgotform"].submit();
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        },
    });


});​