2012-06-02 74 views
3
$("#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; 
       } 
      } 
     }); 
    } 
}); 

對話框#first關閉而不等待#confirm對話框打開。我知道關於JavaScript的confirm()函數,但我想在這種情況下使用對話框。我怎樣才能做到這一點?在jquery中創建確認對話框

回答

3

我要回答我的問題,這是工作的罰款:

$("#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; 
    } 
}); 
+0

爲什麼這個工作會更有幫助的說明。 –

+0

yaa其真實@ muistooshort +1 – gaurav

+0

說明:創建並顯示第一個對話框。當您嘗試關閉它時,會觸發beforeClose事件。在beforeClose事件的處理程序中,顯示輔助「確認」對話框,然後返回值false,以防止第一個對話框關閉。當您點擊標有「確認」的按鈕時,您將手動導航至/ home/index。當您單擊取消時,您將手動關閉確認模式。 – deadbabykitten

8

fine manual

beforeClose(事件,UI)

觸發時,對話即將結束。如果取消,對話框將不會關閉。

所以,你希望你的beforeClose處理程序return false保持對話從收盤:

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

確認按鈕的位置發生變化,所以你不必擔心你的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; 
} 

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