在 sweet alert 上单击确定按钮后如何重定向页面?

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

我可以在页面刷新后显示 sweet alert,但我必须单击 Ok 按钮,我正在进入 sweet alert 以重定向 page.Please 帮助我。

<?php
    echo '<script type="text/javascript">';
    echo 'setTimeout(function () { swal("WOW!","Message!","success");';
    echo '}, 1000);'
    echo 'window.location.href = "index.php";';
    echo '</script>';
?>

要指定回调函数,您必须使用对象作为第一个参数,回调函数作为第二个参数。

echo '<script>
    setTimeout(function() {
        swal({
            title: "Wow!",
            text: "Message!",
            type: "success"
        }, function() {
            window.location = "redirectURL";
        });
    }, 1000);
</script>';

您可以使用内置函数timer,即:

swal({
  title: "Success!",
  text: "Redirecting in 2 seconds.",
  type: "success",
  timer: 2000,
  showConfirmButton: false
}, function(){
      window.location.href = "//whosebug.com/a/37358578/797495";
});
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">

我无法使用任何 swal(sweatAlert) 默认回调函数来做到这一点,所以我强制使用 jquery,得到确定按钮 class 检查 [=26= 中的元素] 做了这样的事情:

<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>

函数 (isConfirm) 中的 'Ok' 警报只是一个测试,看它是否会进入此函数,如果是这样我应该能够从那里重定向,但我没有...

所以我使用 jQuery 来判断 swal 的按钮 "OK" 是否被点击了 class: ".swal2-confirm' 然后我可以成功重定向.. .

希望对大家有所帮助!

PS:我正在使用 php 回显到 运行 脚本,我不必离开 php 到 运行 它,只是使用单引号,你就完成了!

我认为这会有所帮助。和巴默先生给的一样。但我已将其包含在 php 标签中。

开始了....

    <?php if(!empty($_GET['submitted'])):?>
        <script>
        setTimeout(function() {
            swal({
                title: "Congratulaions!",
                text: "Signed up successfully, now verify your mail",
                type: "success",
                confirmButtonText: "Ok"
            }, function() {
                window.location = "index.php";
            }, 1000);
        });
        </script>   
    <?php endif;?>

只需使用 JavaScript 承诺。将 then 方法放在 swal 函数之后。我们不需要使用计时器功能。 例如:

swal({
    title: "Wow!",
    text: "Message!",
    type: "success"
}).then(function() {
    window.location = "redirectURL";
});

promise方法.then用于等待用户读取模态window的信息,一键点击决定做哪个决定。例如,YesNo

点击后,Sweet Alert 可以将用户重定向到另一个屏幕,调用另一个包含新问题和后续问题的 Sweet Alert 模态 window,转到外部 link,等等

同样,我们不必使用计时器,因为它可以更好地控制用户操作。用户可以等待永恒或像 Thanos 或钢铁侠的手指一样采取行动。

通过使用 promises,代码变得更短、更干净、更优雅。

None 以上解决方案对我有用,我不得不使用 .then

swal({
  title: 'Success!',
  text: message,
  type: 'success',
  confirmButtonText: 'OK'
}).then(() => {
  console.log('triggered redirect here');
});

最简单的解决方案,我们也可以添加更多事件!

swal({ title: "WOW!",
 text: "Message!",
 type: "success"}).then(okay => {
   if (okay) {
    window.location.href = "URL";
  }
});
function confirmDetete(ctl, event) {

    debugger;
    event.preventDefault();
    var defaultAction = $(ctl).prop("href");
    swal({
        title: "Are you sure?",
        text: "You will  be able to add it back again!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        cancelButtonText: "Cancel",
        closeOnConfirm: false,
        closeOnCancel: false
    },
        function (isConfirm) {
            if (isConfirm) {
                $.get(ctl);
                swal({
                    title: "success",
                    text: "Deleted",
                    confirmButtonText: "ok",
                    allowOutsideClick: "true"
                }, function () { window.location.href = ctl })

     // $("#signupform").submit();
            } else {
                swal("Cancelled", "Is safe :)", "success");

            }
        });
}

现有答案对我不起作用,我刚刚使用 $('.confirm').hide()。它对我有用。

success: function(res) {
$('.confirm').hide()
swal("Deleted!", "Successfully deleted", "success")
setTimeout(function(){
window.location = res.redirect_url;
},700);

我是用这段代码完成的:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.js"></script>
        <script>

        $(".confirm").on('click',function(){
            window.location.href = "index.php";
        });

        </script>

谢谢。

  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/";   
} 

如果有人需要帮助,此代码有效!

swal({
          title: 'Request Delivered',
          text: 'You can continue with your search.',
          type: 'success'
        }).then(function() {
            window.location.href = "index2.php";
        })

对我有用!!

<head>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
<?php
echo "<script>
Swal.fire({
title: 'Do you want to save the changes?',
showCancelButton: true,
confirmButtonText: `Save`,
denyButtonText: `Don't save`,
}).then((result) => {
if (result.isConfirmed) {
window.location = 'www.google.com';
} else if (result.isDenied) {
Swal.fire('Changes are not saved', '', 'info')
}
})
</script>";
?>

你可以在 SweetAlert2 中使用回调函数,它对我有用。

swal({
    title: "Success!",
    text: "Example success message",
    type: "success"
}, function() {
     window.location.href = 'https://some-url';
});
  $('.delete-record').on('click', function (e) {
            e.preventDefault();
            window.url = $(this).attr('href');
            swal({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Confirm',
                padding: '2em'
            }).then(function (result) {
                if (result.value) {
                    console.log(window.url);
                    window.location.href = window.url;
                }
            });
        });
swal("Payment send successfully", "Thanks for using Npay!", "success")
.then(function() {
    router.push("/")
});

                Swal.fire({
                    title: result.value.title,
                    icon: result.value.icon,
                    text: result.value.message,
                }).then(function() {
                    window.location.href = "url";
                })