Sweet Alert 2在CANCEL上重定向

时间:2018-11-05 19:50:30

标签: laravel sweetalert2

我正在用laravel编写一个项目,在工作流的当前位置,我希望用户能够单击按钮以发布事件或从当前列表中删除事件。

我正在使用SA2停止提交到路由的操作,如果用户单击确定,则一切按计划进行,当用户单击取消时,我想重定向回页面。

我遇到的问题是,当用户单击“取消”时,无论如何都将重定向到页面...

function warnRemove(linkURL) {
    swal({
        title: 'Are you sure?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: 'D6B55',
        confirmButtonText: 'Yes, remove it!'
    }).then(function () {
        window.location = linkURL;
    });
}

function warnPublish(linkURL) {
    swal({
        title: 'Are you sure?',
        type: 'warning',
        text: 'This event will go live on the screen after next refresh.',
        showCancelButton: true,
        confirmButtonColor: 'D6B55',
        confirmButtonText: 'Yes, publish it!'
    }).then(function () {
        window.location = linkURL;
    });
}



$('.remove').click(function(e) {
    e.preventDefault(); // Prevent the href from redirecting directly
    let linkURL = $(this).attr("href");
    warnRemove(linkURL);
});

$('.publish').click(function(e) {
    e.preventDefault(); // Prevent the href from redirecting directly
    let linkURL = $(this).attr("href");
    warnPublish(linkURL);
});

1 个答案:

答案 0 :(得分:3)

您将需要使用带有isConfirm布尔值的回调:

function(isConfirm) {
    if (isConfirm) {
        // do confirmed things
    }
}

从文档中

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then((result) => {
  // redirect only if true
  if (result.value) {
    // redirect here
  }
})