2013-10-15 54 views
4

我用下面的代碼來用jquery ui對話框使用默認的javascript確認。用jquery替換javascript確認確認

jQuery.extend({ 
    confirm: function(message, title, okAction) { 
     jQuery("<div></div>").dialog({ 
      // Remove the closing 'X' from the dialog 
      open: function(event, ui) { jQuery(".ui-dialog-titlebar-close").hide(); }, 
      buttons: { 
       "Ok": function() { 
        jQuery(this).dialog("close"); 
        return true; 
       }, 
       "Cancel": function() { 
        jQuery(this).dialog("close"); 
        return false; 
       } 
      }, 
      close: function(event, ui) { jQuery(this).remove(); }, 
      resizable: false, 
      title: title, 
      modal: true 
     }).text(message); 
    } 
}); 

jQuery.confirm(
     "LogOut", 
     "Do you want to log out", 
     function() { 
     }); 

現在我需要在註銷操作中使用相同的代碼。這樣我就可以替換代碼中的javascript確認。

<a class="homeImage" onclick="return confirm('Do you want to logout?');" href="/future/myhome/head?$event=logout">LOG OUT</a> 

我面對現在的問題是,當我使用其他功能代替確認,並等待其返回值做出決定的問題,對話框不返回的值給一個變量。這兩個函數是同時執行的(它顯示警報,但它也指向href目標)。有沒有什麼方法可以返回一個真或假的值,從而進入href目標。

參考:jQuery ExtendjQuery UI Replacement for alert
相關的問題:js override confirm

+0

問題是,jQuery.confirm()不返回任何東西。這是按鈕功能。 – LazyTarget

+0

我知道,所以我在「確定」和「取消」按鈕中返回true和false,仍然不起作用。所以我怎麼修改我的代碼來返回這些。那是我的問題,你能幫忙嗎? – arjuncc

回答

2

問題是,默認confirm對話框正在同步並阻止整個瀏覽器UI。 JQuery對話框是異步的,不會阻塞UI(因爲它需要呈現)。

所以你的問題的答案如下。您需要更改:

  buttons: { 
      "Ok": function() { 
       window.location = "/future/myhome/head?$event=logout" 
      }, 

<a class="homeImage" onclick="return jQuery.confirm('Do you want to logout?');return false;" href="/future/myhome/head?$event=logout">LOG OUT</a> 
+0

感謝有關「異步」行爲的信息。 – arjuncc

4

我不知道,如果你能真正做到,作爲jQuery.dialog功能是異步的。

您可以使用承諾庫來設置按鈕點擊事件。但你不能簡單地指定要在onclick屬性的方法和必須做它通過代碼

var d = jQuery.Deferred(); 
d.resolve(true); // resolve is used then to mark the function as complete 
return d.promise(); // return the promise 

jsFiddle

jQuery.extend({ 
    confirm: function(message, title, okAction) { 
     var d = jQuery.Deferred(); 
     jQuery("<div></div>").dialog({ 
      // Remove the closing 'X' from the dialog 
      open: function(event, ui) { jQuery(".ui-dialog-titlebar-close").hide(); }, 
      buttons: { 
       "Ok": function() { 
        jQuery(this).dialog("close"); 
        d.resolve(true); 
        return true; 
       }, 
       "Cancel": function() { 
        jQuery(this).dialog("close"); 
        d.resolve(false); 
        return false; 
       } 
      }, 
      close: function(event, ui) { jQuery(this).remove(); }, 
      resizable: false, 
      title: title, 
      modal: true 
     }).text(message); 
     return d.promise(); 
    } 
}); 

有關jQuery的承諾圖書館看到jQuery reference



更多信息編輯:另一種設置方法:jsFiddle

0

就個人而言,我使用確認更多功能的條件執行或提交表單...

因此,使用你的榜樣,我'd做了如下小變動:

buttons: { 
     "Ok": function() { 
       jQuery(this).dialog("close"); 
       okAction(); 
     }, 

然後調用確認如下:

onclick="jQuery.confirm('Do you want to logout?','Confirm:',function(){window.location='/future/myhome/head?$event=logout'}); return false;">LOG OUT</a>