Javascript 自定义配置消息 SweetAlert.js

Javascript custom configrmation message SweetAlert.js

我想在我的 ASP.NET MVC 应用程序中显示自定义确认消息。

经过一番搜索,我发现了 SweetAlert,这是一个非常好的工具。

https://sweetalert2.github.io/

我想在 js 文件中定义一个 javascript 方法,它调用 sweetalert 来显示一个对话框。

但是文档并不等待客户的响应。

为了证明我的意思,我添加了下面的代码。

代码提醒("Doesn't wait");在 sweetalert 显示其消息之前执行。

我的目标是添加自定义 javascript 文件并定义要调用的简单函数和 return true 或 false 以避免在每个确认案例中键入下面的所有代码。 由于不等待客户端交互,不知道可不可以。

有什么想法吗?

    <html>
    <head>
      <script src="SweetAlert.js"></script>
    </head>
    <body>
    <button onclick="customConfirm();">Confirm</button>
    </body>
    </html>

    <script type="text/javascript">

   function customConfirm(){
    Swal.fire({
    title: 'myTitle',
    text: 'my Question',
    type: 'question',
    showCancelButton: true,
    confirmButtonColor: 'rgb(181, 212, 83)',
    cancelButtonColor: '#d33',
    confirmButtonText: 'YES'
   }).then((result) => {
    if (result.value) {
        return true;
    }
    else {
        return false;
    }
   });

   alert("doesn't wait.");
  }

   </script>

您应该在回调中执行所有检查。

   function customConfirm(callback){
    Swal.fire({
    title: 'myTitle',
    text: 'my Question',
    type: 'question',
    showCancelButton: true,
    confirmButtonColor: 'rgb(181, 212, 83)',
    cancelButtonColor: '#d33',
    confirmButtonText: 'YES'
   }).then((result) => {
    // Do some stuff and call the callback
    callback();
    if (result.value) {
        return true;
    }
    else {
        return false;
    }
   });

在另一个文件中:

customConfirm(function() { 
    // Put some stuff you want to do 
}); 

或者:

function callbackDoSomething() {}
customConfirm(callbackDoSomething);