Sweetalert: 如何处理取消按钮动作

创建于 2017-01-25  ·  7评论  ·  资料来源: t4t5/sweetalert

我们有一个用于确认按钮的回调参数,但是我们如何为取消按钮使用回调?

最有用的评论

您可以在文档中找到如何执行此操作的示例: http :

“...通过传递参数,您可以为 'Cancel' 执行其他操作。”

swal({
  title: "Are you sure?",
  text: "You will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  cancelButtonText: "No, cancel plx!",
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm){
  if (isConfirm) {
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
  } else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");
  }
});

所有7条评论

我在代码中看到有一个doneFunction参数。

我正在尝试这段代码,但似乎我的回调被完全忽略了,我做错了什么?

(new Promise((resolve, reject) => {
  swal({
    title: "Delete data?",
    text: "Deleted data will be lost.",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Delete",
    closeOnConfirm: false,
    html: false,
    doneFunction(res) {
      if (res) {
        resolve();
      } else {
        reject();
      }
    }
  });
})).then(() => console.log("Resolved"), () => console.log("Rejected"))

对我来说也是一样。 单击确定按钮时会调用我的回调函数,但不会调用取消按钮。

@jeffmath我切换到https://github.com/limonte/sweetalert2来解决这个问题

您可以在文档中找到如何执行此操作的示例: http :

“...通过传递参数,您可以为 'Cancel' 执行其他操作。”

swal({
  title: "Are you sure?",
  text: "You will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  cancelButtonText: "No, cancel plx!",
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm){
  if (isConfirm) {
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
  } else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");
  }
});

@t4t5通过 isConfirm,我们可以检查用户响应是确认按钮还是其他。 但是你能告诉我如何检查点击的按钮是“确认”按钮还是“取消”按钮或“关闭”按钮,因为我使用取消按钮也重定向到其他地方但关闭按钮只是关闭警报?

实际问题是,在 sweetalert v1.x 中,取消处理程序正在检查以function(开头的回调https://github.com/t4t5/sweetalert/blob/v1.1.3/lib/modules/handle-点击.js#L119。
因此,如果不满足该条件,因为@Zorgatone的示例使用doneFunction(回调将被忽略。

好收获@gualopezb。 我一直在工作,它突然停止工作。

乐修复:

-    }, (code) => {
+    }, function(code) {

我猜我的 Babel 配置发生了一些变化。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

blackrosezy picture blackrosezy  ·  6评论

mouro001 picture mouro001  ·  3评论

sastrahost picture sastrahost  ·  5评论

voodoo6 picture voodoo6  ·  4评论

jamieson99 picture jamieson99  ·  3评论