如何使用 sweetalert 单击确定后重新加载页面

How to reload a page after clicked ok using sweetalert

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

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

这段代码会弹出一条消息,并且有一个按钮okay,我喜欢做的是我想在点击okay按钮后刷新页面。

我可以这样做吗?

你可以试试这个,对我有用。

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

Yoshioka 的回答对我不起作用,我这样做了并且效果很好:

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

对于 Sweet Alert 2,这会起作用。

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

如你所见migration guide

甜蜜警报 2 使用 Promise

你可以通过这个检查确认:

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

我使用 sweet alert 2,这对我有用

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

‘’’ 使用 location.reload() 的答案将触发您的表单一次又一次地尝试重新提交,这就是为什么您应该改用 location.href。

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
   }

})

使用回调函数...

Swal.fire({
  // Swal Setting's
}).then((result) => {
  // Reload the Page
  location.reload();
});
swal({
   title: "Process Completed",
   text: "Data Recorded successfully",
   buttons: true
}).then(function(){
   location.reload();
});

您可以在 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
    }
)

我用这个解决了它:

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

另一种方式:

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

这对我有用。

Swal.fire('Deleted !!', data.message, 'success').then(() => {
    location.reload();
});