2013-06-07 11 views
7

Der folgende Code erscheint ein Bestätigungsfenster auf, wenn der Benutzer löscht Link gedrückt wird:zu Bootbox Bestätigung

<a href="delete_user.php?id=123" onclick="return confirm('Are you sure?');">Delete user</a> 

In diesem Fall, wenn die OK-Taste, um den Link delete_user.php gedrückt wird id = 123 wird hingerichtet. Wenn die Abbrechen-Taste gedrückt wird, passiert nichts.

Ich möchte das gleiche mit Bootbox tun.

<a class="alert" href="list_users.php?id=123">Delete user</a> 

    <script src="bootbox.min.js"></script> 
     <script> 
     $(document).on("click", ".alert", function(e) { 
      e.preventDefault(); 

     bootbox.confirm("Are you sure?", function(result) { 

      if (result) { 
       // What to do here? 
      } else { 
       // What to do here? 
      }    
     }); 

     }); 
    </script> 

Was tun unter if (result) und else Statements?

+0

'if (result) document.location.href = this.attr ('href');'. Kein "sonst" erforderlich. –

+0

Wenn ich this.attr ('href') verwendet habe; Ich bekomme TypeError: this.attr ist keine Funktion. Wenn ich zu $ ​​(this) .attr ('href'); Ich bekomme Undefiniert. Irgendeine Idee? – Oualid

+0

Hm ... was ist, wenn du gerade 'this.href' probiert hast? –

Antwort

19

Das funktionierte für mich. Ergreifen Sie den "Klick" href und verwenden Sie es, wenn Sie "Ergebnis" haben.

<script> 
     $(document).on("click", ".alert", function(e) { 
      var link = $(this).attr("href"); // "get" the intended link in a var 
      e.preventDefault();  
      bootbox.confirm("Are you sure?", function(result) {  
       if (result) { 
        document.location.href = link; // if result, "set" the document location  
       }  
      }); 
     }); 
    </script> 
+0

vielen Dank ... das sollte akzeptiert werden Antwort, es gerettet meinen Tag –

+0

Awesome Dank! Hatte Probleme damit! – user754730

2

Das funktioniert super!

$(".alert").on("click", function (e) { 
    // Init 
    var self = $(this); 
    e.preventDefault(); 

    // Show Message   
    bootbox.confirm("Are you sure?", function (result) { 
     if (result) {     
      self.off("click"); 
      self.click(); 
     } 
    }); 
}); 
+0

Danke, funktioniert großartig –