javascript - 禁用提交按钮取消表单提交

标签 javascript html internet-explorer forms

我有这样一个表格:

<form action="lol.php" method="POST">
<input type="text" name="fe" />
<input type="submit" id="submitbtn" value="submit" onclick="this.disabled = true;" />
</form>

在 Firefox 中它工作得很好,但在 Internet Explorer(最新版本)中按钮被禁用但表单不提交。我也试过:

<form action="lol.php" method="POST" onsubmit="document.getElementById('submitbtn').disabled = true">

并从提交按钮中删除了 onclick 代码,但效果相同。我应该使用什么代码才能在所有浏览器上运行?

最佳答案

好的,好的 hack 是准备第二个什么都不做的假按钮:

<form action="lol.php" method="POST">
<input type="text" name="fe" />
<input type="button" id="submitbtnDisabled" disabled="disabled" value="submit" style="display:none;" />
<input type="submit" id="submitbtn" value="submit" onclick="this.style.display='none'; document.getElementById('submitbtnDisabled').style.display='inline';" />
</form>

注意:

因为您说您没有使用 jQuery,所以我使用的是标准 JavaScript。唯一的建议是使用非侵入式 JavaScript 并将内联 CSS 移至样式表,以将脚本和样式与 HTML 分开。

关于javascript - 禁用提交按钮取消表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3393245/

相关文章:

internet-explorer - Css 在 IE9 预览中旋转

IE9 中的 jquery 事件传播问题

javascript - 开源版本的 Git API

javascript - 为什么 mongodb 不给我超过 100 个文件?

javascript - 错误代码: 402 when trying to capture Web Page with Casperjs

html - 为什么两种字体对于相同的字体大小显示不同高度的文本?

html - 谷歌地图 JavaScript 错误

javascript - 如何使用 Angular 更新绑定(bind)到函数的元素?

javascript - React 的 Hook 调用无效?

internet-explorer - 使用 execScript 时为 "specified cast not valid"