注销OnClientClick不使用Sweetalert

时间:2018-02-16 14:02:22

标签: javascript jquery html asp.net sweetalert

Sweeetalert不像确认框那样工作,它不等待回调函数

当我点击退出时,它会退出而不等待sweetalert是没有确认

<asp:Button runat="server" ID="btnUserlogout" Text="Logout" 
                               OnClick="btnUserlogout_Click"
                               OnClientClick="return UserDeleteConfirmation();"  />


function UserDeleteConfirmation() {
           return  swal({
            title: "Are you sure to Logout? ",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes, I am sure!',
            cancelButtonText: "No, cancel it!",
            closeOnConfirm: false,
            closeOnCancel: false
        });
}

我也试过这个代码

function UserlogoutConfirmation() {
        // return confirm("Are you sure you want to logout?");
       return  swal({
            title: "Are you sure to submit Project? ",
            text: " Once submitted, You will not be able to make any change!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes, I am sure!',
            cancelButtonText: "No, cancel it!",
            closeOnConfirm: false,
            closeOnCancel: false
       }, function (con) {
           if (con) {
               return true
           } else {
               return false
           }
       });

    }

1 个答案:

答案 0 :(得分:2)

你需要使用Promise。 Sweet Alert返回您可以做出反应的承诺。 从Github的官方Sweet Alert文档(https://github.com/t4t5/sweetalert

中查看此示例
swal({
  title: "Are you sure?",
  text: "Are you sure that you want to leave this page?",
  icon: "warning",
  dangerMode: true,
})
.then(willDelete => {
  if (willDelete) {
    swal("Deleted!", "Your imaginary file has been deleted!", "success");
  }
});

替代方案您可以使用异步。这也在Github的文档中有解释。

如果您返回swal函数返回代码,您只需检查用户是否确认如下:

function UserDeleteConfirmation() {
           return  swal({
            title: "Are you sure to Logout? ",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes, I am sure!',
            cancelButtonText: "No, cancel it!",
            closeOnConfirm: false,
            closeOnCancel: false
        });
}

UserDeleteConfirmation().then((isClicked) => {
    //isClicked is true if the button is clicked
}