如果用户单击取消,类型为 "input" 的 SweetAlert 仍会运行

SweetAlert with type of "input" still runs if user clicks cancel

所以我正在使用 SweetAlert,并且提供了向用户请求添加额外短消息的机会。我遇到的问题是,即使用户单击 'Cancel' 按钮,它仍然会在 function().

中发出 post 请求(从下面的示例中删除)
swal({
    title: "Request!",
    text: "Add a message to your request if you wish...",
    type: "input",
    showCancelButton: true,
    closeOnConfirm: false,
    closeOnCancel: true,
    inputPlaceholder: "Short message..."
}, function(inputValue) {
   // this is always run, regardless of if the user clicks cancel
});

我可能可以通过强制输入一条消息来解决这个问题,然后我可以将 post 请求包装在 if 值的 inputValue 语句中,但这必须是可选。

另请注意,我更愿意坚持使用上面链接的 SweetAlert 版本。

如果用户点击取消,回调采用的 inputValue 参数为 false,如果没有点击,则为 true。因此,如果用户点击取消,禁用回调的方法是检查回调中是否存在 inputValue

swal({
    title: "Request!",
    text: "Add a message to your request if you wish...",
    type: "input",
    showCancelButton: true,
    closeOnConfirm: false,
    closeOnCancel: true,
    inputPlaceholder: "Short message..."
}, function(inputValue) {
   if (inputValue) {
      // the user didn't hit cancel
   }
});