How to redirect page after click on Ok button on sweet alert?

前端 未结 12 621
别跟我提以往
别跟我提以往 2020-12-10 01:15

I am able to display sweet alert after the page refresh but I have to click on Ok button which I am getting on sweet alert to redirect the page.Please help me in this.

相关标签:
12条回答
  • 2020-12-10 02:03
      setTimeout(function(){
    Swal.fire({
      title: '<span style="color:#fff">Kindly click the link below</span>',
      width: 600,
      showConfirmButton: true,
        confirmButtonColor: '#ec008c',
        confirmButtonText:'<span onclick="my2()">Register</span>',
      padding: '3em',
      timer: 10000,
      background: '#fff url(bg.jpg)',
      backdrop: `
        rgba(0,0,123,0.4)
        center left
        no-repeat
      `
    })
      }, 1500);
    }
    function my2() {
     window.location.href = "https://www.url.com/";   
    } 
    
    0 讨论(0)
  • 2020-12-10 02:07

    I wasn't able to do that with any swal(sweatAlert) default callback function, so I forced with jquery, got the Ok button class inspecting the element in chrome an made something like this:

    <script>    
              sweetAlert({
                    title:'Warning!',
                    text: 'Invalid user or password!',
                    type:'warning'
              },function(isConfirm){
                    alert('ok');
              });
              $('.swal2-confirm').click(function(){
                    window.location.href = 'index.php';
              });
    </script>
    

    The 'Ok' alert in function(isConfirm) was just a test to see if it would get into this function, if so I should be able to redirect from there, but I wasn't...

    So I used jQuery to determine if the button "OK" of swal was clicked by getting it class: ".swal2-confirm' then I could redirect with success...

    Hope this helps you all !

    PS: I am using php echo to run the script, I din't have to leave php to run it, just use single quotes and you're done !

    0 讨论(0)
  • 2020-12-10 02:15

    Existing answers did not work for me i just used $('.confirm').hide(). and it worked for me.

    success: function(res) {
    $('.confirm').hide()
    swal("Deleted!", "Successfully deleted", "success")
    setTimeout(function(){
    window.location = res.redirect_url;
    },700);
    
    0 讨论(0)
  • 2020-12-10 02:17

    If anyone needs help, this code is working!

    swal({
              title: 'Request Delivered',
              text: 'You can continue with your search.',
              type: 'success'
            }).then(function() {
                window.location.href = "index2.php";
            })
    
    0 讨论(0)
  • 2020-12-10 02:17

    None of the above solutions worked for me, I had to use .then

    swal({
      title: 'Success!',
      text: message,
      type: 'success',
      confirmButtonText: 'OK'
    }).then(() => {
      console.log('triggered redirect here');
    });
    
    0 讨论(0)
  • 2020-12-10 02:19

    Best and Simple solution, we can add more events as well!

    swal({ title: "WOW!",
     text: "Message!",
     type: "success"}).then(okay => {
       if (okay) {
        window.location.href = "URL";
      }
    });
    
    0 讨论(0)
提交回复
热议问题