Sweet Alert 2 和 javascript:如何制作一个甜蜜警报按钮,点击后将我发送到某个网页

Cal*_*oli 2 html javascript jquery sweetalert sweetalert2

到目前为止我的代码:

swal({ 
    title: 'Successfully Registered!', 
    text: "Do you want to go to the Log In page?", 
    type: 'success', 
    showCancelButton: true, 
    confirmButtonColor: '#3085d6', 
    cancelButtonColor: '#d33', 
    confirmButtonText: 'Yes, send me there!' 
    }, 
    function(){ 
       window.location.href= "http://example.com"; 
    }
); 
Run Code Online (Sandbox Code Playgroud)

由于某种原因,window.location.href 不起作用,我尝试只使用location.href 和if(isConfirm) 等
。我该怎么办?
JSFiddle:https ://jsfiddle.net/2dz868kd/

Lim*_*nte 6

以下是使用SweetAlert2 实现的方法

Swal.fire({
    title: 'Successfully Registered!',
    text: 'Do you want to go to the Log In page?',
    icon: 'success',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, send me there!'
 }).then(function(result) {
   if (result.value) {
     location.assign("https://stackoverflow.com/")
   }
 });
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
Run Code Online (Sandbox Code Playgroud)

SweetAlert2 文档:https ://sweetalert2.github.io/

请注意 SweetAlert2 和 SweetAlert 是两个不同的项目。SweetAlert2 使用 ES6 Promises。

  • 伙计,你不知道我有多爱你!我花了太多时间试图弄清楚。 (2认同)