【问题标题】:SweetAlert2 form validation does not returnSweetAlert2 表单验证不返回
【发布时间】:2023-04-10 03:36:01
【问题描述】:

尝试将 SweetAlert2 与表单发送确认一起使用。但无论我怎么尝试,我都无法让它发挥作用。

Swal.fire({
  title: err_msg,
  //html: strCEmail,
  text: 'hello',
  type: 'question',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, send it!'
}).then((result) => {
  if (result.value) {
    // xxxxxxxxxxxxxxxxxxxx
  }
}); 

代码到达 xxxxxxxxxxxxx 很好,但无论我放在那里都不会触发表单提交。

我已经尝试了显而易见的

return true;

但这没有用。然后经过一番挖掘,我发现了一个提交表单的建议:

document.forms["myform"].submit();

或 form.submit();

这没用。

那么...一旦用户在 SweetAlert2 中选择了提交,我可以使用什么来提交表单?

【问题讨论】:

  • 嗯,它是异步的,所以 return 会做废话。使用提交应该可以工作,我猜你应该在控制台中有一条错误消息。
  • 如果您在控制台中打印,您的“结果”会显示什么?
  • 另外,尝试使用文档中的皮卡丘示例“修复”您的代码:sweetalert.js.org/guides/#advanced-examples

标签: javascript forms validation sweetalert2


【解决方案1】:

您很可能正在尝试访问不存在的内容。 基于 SWAL 文档和这篇文章:Response from Sweet-alert confirm dialog 你应该尝试做“if(result)”而不是“if(result.value)”。

另外,正如我上面评论的,你应该阅读文档并阅读神奇宝贝示例,它可能会给你一个提示:sweetalert.js.org/guides/#advanced-examples

【讨论】:

    【解决方案2】:

    为您的表单提供 id

    <form action="your-action" method="post" id="my_form">
    

    在js中:

    Swal.fire({
      title: err_msg,
      text: 'hello',
      type: 'question',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Yes, send it!'
    }).then((result) => {
      if (result.value) {
          $(document).find('#my_form').submit();
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      • 2017-05-02
      • 2017-05-17
      • 2022-01-16
      • 2013-09-22
      相关资源
      最近更新 更多