2014-03-06 85 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; 
    } 
});