2014-03-06 137 views
1
$(document).ready(function() { 
      $("#close").click(function() { 
       var link = $(this).attr("href"); // "get" the intended link in a var 
       var result = confirm("Are you sure?"); 
       if (result) { 
        document.location.href = link; // if result, "set" the document location  
       } 
      }); 
     }); 

如何使用Bootbox對話框而不是JavaScript對話框?將標準Javascript確認對話框轉換爲Bootbox確認

編輯

嗯,我已經試過,但沒有任何反應後點擊OK按鈕bootbox對話框

 $(document).on("click", "#close", function (e) { 
      e.preventDefault(); 
      var link = $(this).attr("href"); // "get" the intended link in a var 
      bootbox.confirm("Are you sure you want to close this Incident? This operation cannot be reversed.", function (result) { 
        if (result) { 
         document.location.href = link; 
        } else { 
         console.log("user declined"); 
        } 
       }); 
     }); 
+1

請參閱編輯 – ASPCoder1450

+0

我的JS調試器中出現一個錯誤,那就是:event.returnValue已棄用。請改用標準的event.preventDefault()。即使即時通訊已經使用preventDefault:s – ASPCoder1450

+0

這不是一個錯誤。它不是由你的代碼造成的。 –

回答

0

我會考慮使用一個Bootbox對話框,如下圖所示。

Bootbox對話框允許您將回調函數附加到按鈕,因此您可以爲每個按鈕指定一個特定的函數。

我還包括行closeButton: false,,以便用戶不能單擊關閉按鈕來關閉對話框,而是必須單擊OkCancel

$(document).on("click", "#close", function (e) { 
    e.preventDefault(); 
    var link = $(this).attr("href"); // "get" the intended link in a var 

    bootbox.dialog({ 
     closeButton: false, 
     message: "Are you sure you want to close this Incident? This operation cannot be reversed.", 
     buttons: { 
      cancel:{ 
       label: "Cancel", 
       callback: doThisOnCancel 
      }, 
      ok:{ 
       label: "Ok", 
       callback: doThisOnOk 
      } 
     }  
    }); 

    doThisOnCancel(){ 
     console.log("user declined");    
    } 

    doThisOnOk(){ 
     document.location.href = link; 
    } 
});