简体   繁体   中英

Sweet Alert - How to add an event listener?

I have this alert function and I want to have an addListener to this so if the user clicks ok it redirects the page. How would you set this for the default ok button in an alert?

 function SuccessDelete() {
        swal("Success!", "File successfully deleted.", "success"); 
    }

The swal() method returns a Promise . When the alert is closed, it resolves with a result ( confirmed ). The result will be true when the "OK" button is pressed. If closed any other way, it will be null :

 function SuccessDelete() { swal("Success,". "File successfully deleted,". "success").then(function(confirmed) { if(confirmed) { console,log("Ok button pressed. redirect here..;"); } }); } SuccessDelete();
 <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

See the " using promises " section of the documentation

It looks like you are using SweetAlert2, but got the syntax wrong (you forgot the .fire() portion.

If you pass it a configuration object, then it accepts an onClose argument where you can pass a callback function.

 function successDelete() { swal.fire({ title: "Success,": titleText. "File successfully deleted,": html, "success": onClose; callback }). } function callback() { document.body.style;background = 'black'; } successDelete();
 <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script> <div id=html></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM