You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SweetAlert弹窗确认后表单无法提交问题求助

问题分析与解决方法

你的代码核心问题有两个:

  • 错误地将submit事件绑定到了提交按钮(.swa-confirm)上,而非表单本身,导致事件拦截逻辑混乱。
  • 调用$myForm.submit()时,会再次触发jQuery绑定的表单submit事件,形成循环,无法完成真实提交。

修正步骤:

  1. 调整事件绑定目标:将事件绑定到表单(.verbale)的submit事件,这样才能正确拦截表单的提交动作。
  2. 使用原生DOM提交方法:在SweetAlert的确认回调中,调用表单DOM元素的原生submit()方法,绕过jQuery的事件监听,避免循环触发。

修改后的完整代码:

<form id="verbale" class="verbale" action="editor_conferma.php" method="post">
...
<input id="go" type="submit" name="go" value="go" class="swa-confirm">
</form>

<script>
$(document).on("submit", ".verbale", function (e) {
    e.preventDefault();
    var $myForm = $(this); // 直接用当前表单,无需重新选择
    if (!$myForm[0].checkValidity()) {
        $myForm[0].reportValidity()
    } else {
        swal({
            title: "Are you sure?",
            text: "Did you check?",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "Yes, Submit!",
        }).then(function (result) {
            // 使用原生DOM方法提交,避免触发jQuery的submit事件
            $myForm[0].submit();
        });
    }
});
</script>

额外说明:

  • 用$(this)代替重新选择.verbale,代码更简洁且避免重复DOM查询。
  • 原生submit()方法不会触发jQuery绑定的submit事件,所以能直接绕过之前的拦截逻辑,完成表单提交。

内容的提问来源于stack exchange,提问作者Alessandro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 11:43:14