在jquery中创建确认对话框

Gau*_*rav 3 javascript jquery jquery-ui jquery-ui-dialog

$("#first").dialog({ width: 304, modal: true,

  beforeclose: function (e, ui) 
  {
        $("#confirm").dialog({ width: 500, modal: true,
            buttons: {
                "Confirm": function () {
                    document.location.href = "/Home/Index";
                },
                "Cancel": function () {
                    $(this).dialog('close');
                    return false;
                }
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

对话框#first关闭,无需等待#confirm对话框打开.我知道confirm()javascript的功能,但我想在这种情况下使用对话框.我怎样才能做到这一点?

mu *_*ort 8

精细手册:

beforeClose(event,ui)

对话框即将关闭时触发.如果取消,对话框将不会关闭.

所以你希望你的beforeClose处理程序return false阻止对话关闭:

beforeClose: function(e, ui) {
    $("#confirm").dialog({ width: 500, modal: true, /* ... */ });
    return false;
}
Run Code Online (Sandbox Code Playgroud)

您的" 确认"按钮会更改位置,因此您不必担心beforeClose处理程序会阻止第二个对话框关闭第一个对话框.如果您没有更改页面位置,那么您需要某种标志以beforeClose防止所有关闭; 像这样的东西,例如:

beforeclose: function(e, ui) {
     var $dlg = $(this);
     if($dlg.data('can-close')) {
         $dlg.removeData('can-close');
         return true;
     }
     $("#confirm").dialog({
         //...
         buttons: {
             Confirm: function() {
                 $(this).dialog('close');
                 $dlg.data('can-close', true);
                 $dlg.dialog('close');
             },
             Cancel: function() {
                 $(this).dialog('close');
             }
         }
     });
     return false;
 }
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/ambiguous/jYZpD/