【问题标题】:Sweet Alert 2 redirect on CANCELSweet Alert 2 重定向取消
【发布时间】:2018-11-05 19:50:29
【问题描述】:

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

我正在使用SA2在它发生之前停止提交到路由,如果用户点击OK,那么一切都按计划进行,当用户点击取消时,我想重定向回页面。

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

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

【问题讨论】:

    标签: laravel sweetalert2


    【解决方案1】:

    您需要使用带有 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
      }
    })
    

    【讨论】:

    • 完美运行,isConfirm 不再适用于 SA2,但底部块成功了!
    猜你喜欢
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    相关资源
    最近更新 更多