2013-04-25 103 views
1

我試圖用下面的代碼打開一個Jquery確認框。Jquery確認對話框的返回值

var a = $('#confirm')     
      .data("x","defaultValue") 
      .dialog('open'); 

alert(a.data("x")); 

在對話框中,我試圖改變x的值。

$("#confirm").dialog({ 
    resizable: false, 
    autoOpen: false, 
    height: 180, 
    width: 400, 
    modal: true, 
    buttons: { 
     "Leave the page": function() {     
      $(this).data("x","this is a test");     
      $(this).dialog("close"); 
     }, 
     Cancel: function() { 
      $(this).dialog("close"); 
     } 
    } 
}); 

如何獲取x的修改值。此時警報顯示「DefaultValue」。但是想要得到「這是一個測試」。

有什麼想法?

PS:我只是不能使用對話框中的window.open()重定向。

回答

0

你不能,我的意思是至少在對話框外面,「alert」在「離開頁面」之前執行。您需要在「對話關閉」呼叫後立即提醒。

$(this).data("x","this is a test"); 
$(this).dialog("close"); 
alert($(this).data("x")); 
1

終於設法解決了這個問題。只是想在這裏發佈,如果有人需要它。

$("#confirm").dialog({ 
    resizable: false, 
    autoOpen: false, 
    height: 180, 
    width: 400, 
    modal: true, 
    buttons: { 
     "Leave the page": function() { 
      var anchorId = $(this).data("anchorId"); 
      var formId = $(this).data("formId"); 

      if (typeof anchorId !== "undefined") 
       window.location.assign(anchorId); 
      if (typeof formId !== "undefined") 
       $(formId).submit(); 
      $(this).dialog("close"); 
     }, 
     Cancel: function() { 
      $(this).dialog("close"); 
     } 
    } 
}); 

我打開確認對話框

$("a").live("click", function() {  
    if (hasUnsavedData()) { 
     $('#confirm') 
     .data("anchorId", this) 
     .dialog('open'); 
     return false; 
    } else return true; 
});