javascript - SweetAlert 配置困惑

标签 javascript sweetalert

我尝试使用具有以下配置的 SweetAlert 2.1.2:

swal({
    title: "My Event Title",
    text: "My event message",
    icon: "warning",
    html: true,
    closeOnClickOutside: true,
    content: {
      element: "input",
      attributes: {
        placeholder: "User text",
        type: "input"
      }
    },
    buttons: {
      cancelMove: {
        text: "Cancel",
        value: "c"
      },
      sendConfirm: {
        text: "Yes, continue",
        value: "s"
      },
    }
  },
  function(value) {
    switch (value) {

      case "cancelMove":
        swal("Move cancelled");
        break;

      case "sendConfirm":
        swal("Sending confirmation ...");
        break;

      default:
        swal("Good bye!!!");
    }
  });

但是,我没有得到文本输入和两个按钮的预期结果: enter image description here

我认为我正确地遵循了他们的配置步骤。但是,显然我不是。

最佳答案

在使用其定义的值单击按钮期间,您将传入此按钮值的 value 。我的意思是 cancelMove 发送 value = csendConfirm 发送 value = s

还可以使用 .then( 而不是 }, function(.

下面的工作片段:

swal({
    title: "My Event Title",
    text: "My event message",
    icon: "warning",
    html: true,
    closeOnClickOutside: true,
    content: {
      element: "input",
      attributes: {
        placeholder: "User text",
        type: "input"
      }
    },
    buttons: {
      cancelMove: {
        text: "Cancel",
        value: "c"
      },
      sendConfirm: {
        text: "Yes, continue",
        value: "s"
      },
    }
  }).then((value)=>{
 
    switch (value) {

      case "c":
        swal("Move cancelled");
        break;

      case "s":
        swal("Sending confirmation ...");
        break;

      default:
        swal("Good bye!!!");
    }
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

关于javascript - SweetAlert 配置困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59041098/

相关文章:

r - 在 R Shiny 中使用 SweetAlert2

javascript - Laravel 5.2 - Sweet Alert 确认框

javascript - 甜蜜警报不起作用

javascript - Backbone : is 100 + Views ok?

javascript - 引用错误 : documentFragmentChildren is not defined

javascript - 获取 Javascript 中的闭包变量列表

javascript - 构建 "queryable"javascript/JSON 对象

javascript - Sweetalert 包和 jQuery submit() 无法正常工作

javascript - 在 sweet Alert 中运行 jquery

javascript - jQuery,附加 HTML 字符串不起作用