简体   繁体   English

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

[英]Disabling submit button cancels form submit

I have a form like this: 我有一个这样的表格:

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

and in Firefox it works perfectly, but in Internet Explorer (latest version) the button becomes disabled but the form does not submit. 并且在Firefox中它可以很好地工作,但在Internet Explorer(最新版本)中,该按钮被禁用但表单未提交。 I have also tried: 我也尝试过:

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

and removed the onclick code from the submit button, but that had the same effect. 并从提交按钮中删除了onclick代码,但效果相同。 What code should I use so that it works on all browsers? 我应该使用什么代码才能在所有浏览器上运行?

Ok, so the good hack is to prepare second fake button that does nothing: 好的,所以好的黑客是准备第二个什么都不做的假按钮:

<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>

Note: 注意:

Because you said you're not using jQuery I'm using standard JavaScript. 因为你说你没有使用jQuery我正在使用标准的JavaScript。 The only suggestion is to use unbotrusive JavaScript and move inline CSS to a stylesheet just to separate scripts and styles from HTML. 唯一的建议是使用unbotrusive JavaScript并将内联CSS移动到样式表,只是为了将脚本和样式与HTML分开。

I ran into this problem before as well. 我之前也遇到过这个问题。 I ended up hiding the submit button when the form was submitted, and replacing it with a disabled button with the same text. 我在提交表单时最终隐藏了提交按钮,并将其替换为具有相同文本的禁用按钮。 Worked great. 工作得很好。

try this: 试试这个:

onclick="this.disabled=true;return true;"

Otherwise, MS exploder is not allowing the button to continue processing once disabled. 否则,MS exploder不允许按钮在禁用后继续处理。 (It's a bitch, cause I use this and it works in FireFox) (这是一个婊子,因为我使用它并且它适用于FireFox)

That's weird. 那真是怪了。

Try: 尝试:

<input type="submit" id="submitbtn" value="submit" onclick="setTimeout(function () { this.disabled = true;}, 0)" />

If you want you can use jquery. 如果你想要你可以使用jquery。

Try this: 试试这个:

$('#submtbtn').submit(function() {
   $(this).attr('disabled', 'disabled');
});

I think it works on all browser. 我认为它适用于所有浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM