开发者

Why clicking a disabled submit button submits the form in Internet Explorer 8 only?

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 开发者_如何学PythonFirefox 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 ?


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

        function deleteButtonClicked(button){ /*pass button reference*/
          if(button.disabled == false){ /*only do something when button is enabled*/
            $('#action').val("delete"); /*in case of multiple submit buttons, indicate which button is used*/
            $('#MyForm').submit(); /*call the form-submit manually*/
            }
        }

Use the "onclick"-parameter, for example, to trigger your script.

    <form name="MyForm" ...etc etc...>
    <input type="button" name="delBtn" value="Click here to delete" onclick="deleteButtonClicked(this)">
    </form>

Hope this helps.


Try putting a return false on the submit event. For example:

$('form').submit(function() {
  return false;
});

Hope that helps!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜