为什么单击禁用的提交按钮只能在 Internet Explorer 8 中提交表单?
在我的 Rails 3 应用程序中,我有以下形式:
<%= form_tag("/lounge/add_new_message", :id => "new_message_form", :remote => true) do %>
<%= text_area_tag("new_message_area", nil, :maxlength => 1000) %>
<%= submit_tag("Send", :id => "new_message_button", :disabled => true) %>
<% end %>
页面加载时按钮被禁用。
但是,当在 Internet Explorer 8 中单击禁用按钮时,会提交表单。在 Firefox 4 和 Chrome 10 中,不会提交表单。
我添加了以下 JavaScript 来验证该按钮确实已禁用:
$('form').submit(function() {
alert($('#new_message_button').attr("disabled"));
});
并且它确实在警报框中显示“true”。
我尝试创建一个最小的示例来展示这一点,但没有成功。
造成这种奇怪行为的原因是什么?
In my Rails 3 application I have the following form:
<%= form_tag("/lounge/add_new_message", :id => "new_message_form", :remote => true) do %>
<%= text_area_tag("new_message_area", nil, :maxlength => 1000) %>
<%= submit_tag("Send", :id => "new_message_button", :disabled => true) %>
<% end %>
The button is disabled on page load.
However, when the disabled button is clicked in Internet Explorer 8, the form is submitted. In Firefox 4 and Chrome 10 the form is not submitted.
I added the following JavaScript to verify that the button is indeed disabled:
$('form').submit(function() {
alert($('#new_message_button').attr("disabled"));
});
and it indeed displays "true" in the alert box.
I tried to create a minimal example to show this in action, but with no success.
What could be the reason for such a strange behavior ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是 IE8 中的某种错误。我无法使用 Christian 的解决方案,因为我的应用程序中的另一个脚本只允许我发出一个提交,并且我必须等待服务器的响应才能提交另一个提交(在 Christian 的情况下永远不会发生)。所以我通过不使用提交按钮,而是使用普通按钮并调用 javascript 方法(也使用 JQuery)解决了这个问题。
例如,使用“onclick”参数来触发脚本。
希望这有帮助。
It seems to be some kind of bug in IE8. I couldn't use Christian's solution, because another script in my application only allowes me to issue one submit and I have to wait for the server's response in order to submit another one (which in Christian's case will never happen). So I resolved the problem by not using a submit-button, but a normal button and calling a javascript method (also using JQuery).
Use the "onclick"-parameter, for example, to trigger your script.
Hope this helps.
尝试在提交事件上添加return false。例如:
希望有帮助!
Try putting a return false on the submit event. For example:
Hope that helps!
我想知道 - 这可能是由于远程表单中按钮上的“onclick”事件造成的吗?禁用的按钮仍然是“可点击的”,并且可能会将其传递给表单提交功能。
I wonder - Could this be due to an 'onclick' event on a button in a remote form? Disabled buttons are still 'clickable' and it may be passing it to the form submit function.