Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在sweet alert上单击Ok按钮后如何重定向页面?_Javascript_Jquery_Sweetalert - Fatal编程技术网

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

Javascript 在sweet alert上单击Ok按钮后如何重定向页面?,javascript,jquery,sweetalert,Javascript,Jquery,Sweetalert,我可以在页面刷新后显示sweet alert,但我必须单击确定按钮,我将进入sweet alert以重定向页面。请在这方面帮助我 <?php echo '<script type="text/javascript">'; echo 'setTimeout(function () { swal("WOW!","Message!","success");'; echo '}, 1000);' echo 'window.location.href =

我可以在页面刷新后显示sweet alert,但我必须单击确定按钮,我将进入sweet alert以重定向页面。请在这方面帮助我

<?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>';
echo'
setTimeout(函数(){
游泳({
标题:“哇!”,
短信:“留言!”,
类型:“成功”
},函数(){
window.location=“重定向URL”;
});
}, 1000);
';

您可以使用内置功能
定时器,即:

swal({
标题:“成功!”,
文本:“2秒后重定向。”,
键入:“成功”,
计时器:2000,
showconfixton:false
},函数(){
window.location.href=“//stackoverflow.com”;
});

我无法使用任何swal(sweatAlert)默认回调函数实现这一点,因此我强制使用jquery,让Ok button类检查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>

甜心({
标题:“警告!”,
文本:“无效的用户或密码!”,
类型:'警告'
},功能(isConfirm){
警报(“正常”);
});
$('.swal2 confirm')。单击(函数(){
window.location.href='index.php';
});
函数中的“Ok”警报(isConfirm)只是一个测试,看看它是否会进入这个函数,如果是的话,我应该能够从那里重定向,但我没有

因此,我使用jQuery确定是否通过获取其类来单击swal的“OK”按钮:“.swal2确认”,然后我可以成功重定向

希望这对大家都有帮助


PS:我正在使用php echo来运行脚本,我不必离开php来运行它,只需使用单引号就可以了!

我认为这会有所帮助。这与Barmer先生给出的相同。但我已将其包含在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;?>

setTimeout(函数(){
游泳({
标题:“祝贺你们!”,
文本:“注册成功,现在验证您的邮件”,
键入:“成功”,
confirmButtonText:“好的”
},函数(){
window.location=“index.php”;
}, 1000);
});

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

swal({
    title: "Wow!",
    text: "Message!",
    type: "success"
}).then(function() {
    window.location = "redirectURL";
});
promise方法
。然后使用
等待,直到用户读取模式窗口的信息,并通过单击一个按钮来决定要做出的决定。例如,
Yes
No

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


再次强调,我们不必使用定时器,因为控制用户的动作要好得多。用户可以等待永恒,也可以像萨诺斯或铁人的手指折断一样采取行动。上述解决方案对我都不起作用,我不得不使用。然后

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

现有答案对我不起作用,我只是使用了
$('.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>

$(“.confirm”)。在('click',function()上{
window.location.href=“index.php”;
});
谢谢。

设置超时(函数(){
  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/";   
} 
喷火({ 标题:“请点击下面的链接”, 宽度:600, showConfirmButton:没错, confirmButtonColor:“#ec008c”, confirmButtonText:“注册”, 填充:“3em”, 计时器:10000, 背景:“#fff url(bg.jpg)”, 背景:` rgba(0,0123,0.4) 中间偏左 不许重复 ` }) }, 1500); } 函数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";
        })


您能否提供
swal
function@DININDU它来自SweetAlert插件。标签上有一个指向文档的链接。感谢您的回复Barmar先生,您通过代码检查了吗?我必须在echo中显示该功能““。你能帮助我如何在echo中编写代码吗?当你使用
echo
编写代码时没有区别。没有echo,代码可以正常工作,但当我插入en echo时,它不工作。我必须在页面刷新时加载sweet alert。”。我的代码正在刷新页面,但“确定”按钮不起作用。对不起,Barmar先生,代码不起作用。页面完全空白。Pedro先生,您按代码检查了吗?我必须在echo“”中显示该函数。您能帮助我如何在echo中编写代码吗?它不重定向。请使用chrome检查元素,如果它不重定向,可能会在控制台上返回错误。还记得正确添加sweetalert(和jquery)脚本和CSS。*更正:我使用的是一个较旧的swal文件,只需更新到最新的文件,该函数应该可以工作,正如@PedroLobito在下面解释的那样。我到处查看,直到看到这篇文章,没有任何建议起作用;工作得很有魅力。谢谢除非我能在IE中使用这个该死的东西。有人知道为什么不执行回调函数吗?这不是一个好答案,因为问题已经回答了,你应该编辑@Barmer的答案并用PHP标记更新它。是的,你是对的。我很抱歉。我不太熟悉这个网站上的提问和回复,它不会再发生了,谢谢你指出这一点。未捕获的TypeError:无法为我读取未定义的属性“then”!为了激发swal,我使用了:
swal.fire({…}).then(function(){…})。这确实是一条路。
swal({
          title: 'Request Delivered',
          text: 'You can continue with your search.',
          type: 'success'
        }).then(function() {
            window.location.href = "index2.php";
        })