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

点击提交按钮时confirm返回false,表单仍提交的问题排查

表单提交确认弹窗失效问题解决办法

你的问题核心是没有将confirm_check()函数的返回值关联到表单提交的阻止逻辑上。虽然confirm()在点击Cancel时返回了false,但这个结果没有被用来中断表单的默认提交行为,所以表单还是会被提交。

快速修复方案

直接修改提交按钮的onclick属性,让它返回函数的执行结果:

<input type="submit" accesskey="s" tabindex="6" name="post" onclick="return confirm_check();" value="Submit" class="button1 default-submit-action">&nbsp;

当confirm()返回true时,return true会允许表单正常提交;返回false时,return false会阻止表单的默认提交行为,达到你想要的效果。

更规范的进阶写法(推荐)

内联事件属性(如onclick)会让JS代码和HTML结构耦合,推荐使用事件监听的方式实现:

  1. 先移除按钮上的onclick属性:
<input type="submit" accesskey="s" tabindex="6" name="post" value="Submit" class="button1 default-submit-action">&nbsp;
  1. 修改<script>里的代码为:
document.addEventListener('DOMContentLoaded', function() {
  // 找到提交按钮
  const submitBtn = document.querySelector('input[name="post"]');
  // 添加点击事件监听
  submitBtn.addEventListener('click', function(event) {
    const isConfirmed = confirm('Does this post conform to the board rules? If unfamiliar with the board rules, click Cancel then read the Board rules on the navigation bar. If it complies, submit the form again and select OK.');
    if (!isConfirmed) {
      event.preventDefault(); // 阻止表单默认提交行为
    }
  });
});

这种写法将逻辑和结构分离,更便于维护和扩展。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 14:45:29