2013-04-25 101 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; 
});