如何在点击“确定”后使用sweetalert重新加载页面

11

你好,我有一个使用sweetalert的代码

swal("Good job!", "You clicked the button!", "success")

这段代码将弹出一个消息框,并有一个“好的”按钮,我想要做的是在单击“好的”按钮后刷新页面。

我可以这样做吗?

11个回答

32

你可以尝试这个方法,它对我有效。

swal({
       title: "Good job", 
       text: "You clicked the button!", 
       type: "success"
     },
   function(){ 
       location.reload();
   }
);

28

Yoshioka的答案对我没有用,但我尝试了以下方法,它完美地解决了我的问题:

swal({title: "Good job", text: "You clicked the button!", type: 
"success"}).then(function(){ 
   location.reload();
   }
);

2
Yoshioka的答案对我没用,但Leo感谢它有效。 - Vinay Kumar

15

使用回调函数来处理异步操作结果。

Swal.fire({
  // Swal Setting's
}).then((result) => {
  // Reload the Page
  location.reload();
});

5
您可以通过以下方式进行确认:
swal({
  title: "Good job",
  text: "You clicked the button!",
  icon: "success",
  buttons: [
    'NO',
    'YES'
  ],
}).then(function(isConfirm) {
  if (isConfirm) {
    location.reload();
  } else {
    //if no clicked => do something else
  }
});

5

我使用SweetAlert2,这对我有用。

swal("Good job!", "You clicked the button!","success").then( () => {
    location.href = 'somepage.html'
})

使用location.reload()的答案会导致您的表单尝试一遍又一遍地重新提交,因此您应该使用location.href。


5
对于 Sweet Alert 2,这将起作用。
swal("Good job!", "You clicked the button!", "success").then(function(){
    location.reload();
});

正如您所看到的迁移指南

Sweet Alert 2使用Promise


2
Sweet Alert 2中,有回调函数可以实现您的逻辑:
Swal.fire({
  title: 'Great job',
  text: "You clicked the button!",
  type: 'success',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes'
}).then((result) => {
   if(result){
     // Do Stuff here for success
     location.reload();
   }else{
    // something other stuff
   }

})

1
这对我很有帮助。
Swal.fire('Deleted !!', data.message, 'success').then(() => {
    location.reload();
});

这应该是被接受的答案。在Sweetalert2中,它能够完美运行。 - Ajay Kumar

1

您可以在SweetAlert2上找到参考资料。

Swal.fire(
    {
        title: "Good job", 
        text: "You clicked the button!", 
        type: "success",
        showDenyButton: true,          // In case you want two scenarios 
        denyButtonText: 'ABC',
        showCancelButton: true,        // In case you want two scenarios 
        cancelButtonText:'XYZ'
    }
).then(function (result) {
    if (result.isConfirmed) {
        //You can add code here if user pressed ok button
    } else if (result.isDenied) {
        //You can add code here if user pressed deny button
    } else if(result.isDismissed) {
        //You can add code here if user pressed cancel button
    }
)

0

我用以下代码解决了这个问题:

swal({title: "Updated!", text: "yourText", type: "success"},function() {
                                location.reload();
     }) // swal end 

另一种方式:

swal("Updated!","yourText","success",function() {
     location.reload();
}) // swal end

这非常类似于上面的注释。 - Fil
我也从这里尝试过。我用那种方式解决了它。之后,我想我应该分享这种方法给@Fil。 - Ripon Uddin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接