单击甜蜜警报上的确定按钮后如何重定向页面?

时间:2016-05-21 02:57:09

标签: javascript jquery sweetalert

我能够在页面刷新后显示甜蜜警报,但是我必须单击“确定”按钮,我会在甜蜜的警报上重定向页面。请帮助我。

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

13 个答案:

答案 0 :(得分:23)

只需将.then(放在函数前面。我们需要使用计时器功能。 例如:

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

.then功能用于等待用户读取模态窗口的信息,并通过单击一个按钮决定做出哪个决定。例如,YesNo

点击后,Swich Alert可以将用户重定向到另一个屏幕,调用另一个Sweet Alert模式窗口,其中包含新的和随后的问题,转到外部链接等等。

同样,我们不必使用计时器,因为控制用户操作要好得多。用户可以等待永恒或采取行动作为Thanos的手指按扣。

使用.then,代码变得更短,更干净,更优雅。 ;)

答案 1 :(得分:17)

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

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

答案 2 :(得分:6)

setTimeout(function () { 
swal({
  title: "Wow!",
  text: "Message!",
  type: "success",
  confirmButtonText: "OK"
},
function(isConfirm){
  if (isConfirm) {
    window.location.href = "//stackoverflow.com";
  }
}); }, 1000);
<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">

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

swal({
  title: "Success!",
  text: "Redirecting in 2 seconds.",
  type: "success",
  timer: 2000,
  showConfirmButton: false
}, function(){
      window.location.href = "//stackoverflow.com";
});
<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">

答案 3 :(得分:5)

我无法使用任何swal(sweatAlert)默认回调函数执行此操作,因此我强制使用jquery,使用Ok按钮类检查chrome中的元素,如下所示:

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

&#39; Ok&#39;函数中的警报(isConfirm)只是一个测试,看它是否会进入这个函数,如果是这样我应该可以从那里重定向,但我不是......

所以我使用jQuery来确定按钮是否正常&#34;通过上课来点击swal:&#34; .swal2-confirm&#39;然后我可以成功重定向......

希望这能帮到你们所有人!

PS:我正在使用php echo来运行脚本,我不得不让php运行它,只需使用单引号即可完成!

答案 4 :(得分:4)

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

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

答案 5 :(得分:2)

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

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

答案 6 :(得分:0)

我认为这会有所帮助。它与巴尔默先生给出的相同。但我在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;?>

答案 7 :(得分:0)

以上解决方案均不适合我,我必须使用 .then

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

答案 8 :(得分:0)

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");

            }
        });
}

答案 9 :(得分:0)

现有答案对我不起作用,我只使用了2018-10-08 02:50:07.055 21984-22263/com.ggstudios.lolcatalyst E/AndroidRuntime: FATAL EXCEPTION: RxComputationThreadPool-2 Process: com.ggstudios.lolcatalyst, PID: 21984 java.lang.Throwable: Unable to resolve host "mywebsite.com": No address associated with hostname at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:157) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105) at java.net.InetAddress.getAllByName(InetAddress.java:1154) at okhttp3.Dns$1.lookup(Dns.java:40) at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185) at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149) at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:214) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) at okhttp3.RealCall.execute(RealCall.java:77) at com.ggstudios.lolcatalyst.offline.OfflineDownloadViewModel$downloadImage$1.subscribe(OfflineDownloadViewModel.kt:372) at io.reactivex.internal.operators.completable.CompletableCreate.subscribeActual(CompletableCreate.java:39) at io.reactivex.Completable.subscribe(Completable.java:1918) at hu.akarnokd.rxjava2.debug.CompletableOnAssembly.subscribeActual(CompletableOnAssembly.java:39) at io.reactivex.Completable.subscribe(Completable.java:1918) at io.reactivex.internal.operators.completable.CompletableSubscribeOn$SubscribeOnObserver.run(CompletableSubscribeOn.java:64) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:38) at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:26) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.Throwable: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname) at libcore.io.Linux.android_getaddrinfo(Native Method) at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:172) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:137) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)  at java.net.InetAddress.getAllByName(InetAddress.java:1154)  at okhttp3.Dns$1.lookup(Dns.java:40)  at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)  at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)  at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)  at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:214)  at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)  at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)  at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)  at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)  at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)  at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)  at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)  at okhttp3.RealCall.execute(RealCall.java:77)  at com.ggstudios.lolcatalyst.offline.OfflineDownloadViewModel$downloadImage$1.subscribe(OfflineDownloadViewModel.kt:372)  at io.reactivex.internal.operators.completable.CompletableCreate.subscribeActual(CompletableCreate.java:39)  at io.reactivex.Completable.subscribe(Completable.java:1918)  at hu.akarnokd.rxjava2.debug.CompletableOnAssembly.subscribeActual(CompletableOnAssembly.java:39)  at io.reactivex.Completable.subscribe(Completable.java:1918)  at io.reactivex.internal.operators.completable.CompletableSubscribeOn$SubscribeOnObserver.run(CompletableSubscribeOn.java:64)  at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:38)  at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:26)  at java.util.concurrent.FutureTask.run(FutureTask.java:266)  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)  at java.lang.Thread.run(Thread.java:764)  。对我有用。

$('.confirm').hide()

答案 10 :(得分:0)

我通过使用以下代码来做到这一点:

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

谢谢。

答案 11 :(得分:0)

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

答案 12 :(得分:0)

为我工作!!

<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>";
?>