首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript自定义配置消息SweetAlert.js

Javascript自定义配置消息SweetAlert.js
EN

Stack Overflow用户
提问于 2019-12-05 23:02:17
回答 1查看 121关注 0票数 0

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

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

https://sweetalert2.github.io/

我想在一个js文件中定义一个javascript方法,它会调用这个甜蜜的警告来显示一个对话框。

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

为了演示我的意思,我添加了下面的代码。

代码alert(“its‘t wait");在sweetalert显示其消息之前执行。

我的目标是添加一个自定义的javascript文件,并定义一个简单的函数来调用并返回true或false,以避免在每个确认案例中输入下面所有的代码。因为它不等待客户端的交互,所以我不知道这是否可能。

有什么想法吗?

代码语言:javascript
复制
    <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-05 23:06:53

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

代码语言:javascript
复制
   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;
    }
   });

在另一个文件中:

代码语言:javascript
复制
customConfirm(function() { 
    // Put some stuff you want to do 
}); 

或者:

代码语言:javascript
复制
function callbackDoSomething() {}
customConfirm(callbackDoSomething);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59197920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档