views:

29

answers:

2

The full story is I want to disable the submit button on a form's submission to prevent multiple submissions. One of the problems is the form is an ASP.Net MVC Ajax form - <% using (Ajax.BeginForm ... %> - so its javascript onSubmit event is already set and I therefore cannot do this during that event. Unless there's a way to add to the submit event with jQuery? My testing shows that $("#form").submit(...) replaces the submit event and thusly kills MVC's ajaxy stuff.

So what I've done is attached the button-disabling code to the submit button's click event. That works fine ... only it kills the button's submission abilities (nothing happens other than the button gets disabled). I took it a step further and used jQuery to submit the form from the click event. This borks everything, the ajax response comes back as a downloaded file, I assume because MVC's ajax stuff got clobbered again. This is where I'm at:

$("#submitButton").click(function ()
{
    var $button = $(this);
    $button.parent().submit();
    $button.attr("disabled", "disabled");
});

Any one know a work-around?

Edit: So, turns out this actually may be a bug in MVC. Originally I had:

<% using (Ajax.BeginForm("CreateAsync", "Account", new AjaxOptions
    {
        HttpMethod = "POST",
        OnComplete = "create_Complete",
        OnBegin = "create_Begin"
    },
    new { id = "ajaxForm" }))
{ %>
<!-- form stuff -->
<% } %>

$("#ajaxForm").submit(function() {
    $(":submit", this).attr("disabled", "disabled");
});

Notice I was setting the form's ID and grabbing a jQuery handle to it via that ID. Looks like the simple fact that I'm setting the ID of the form is causing all the issues. In fact, I can break the form submission w/o any jQuery intervention what-so-ever if I set the form ID. So this works totally as advertised:

<% using (Ajax.BeginForm("CreateAsync", "Account", new AjaxOptions
    {
        HttpMethod = "POST",
        OnComplete = "create_Complete",
        OnBegin = "create_Begin"
    }))
{ %>
<!-- form stuff -->
<% } %>

$("form").submit(function() {
    $(":submit", this).attr("disabled", "disabled");
});
+1  A: 

Basically something like

$("form").submit(function() {
    $(this).submit(function() { return false; });
    $(":submit", this).addClass("PseudoDisabled");
});

attached on document.ready works well in our project. As you cannot actually disable the submit button or the postback will be ignored. We basically created a disabled style that is applied onSubmit that gives the appearance of disabled and prevents multiple submits by attached a new submit event handler.

Calgary Coder
Very nice, although I suggest an `unbind` to remove useless event handlers.
digitalFresh
@digitalFresh - Probably not a bad idea to be safe.
Calgary Coder
Ah, I see, very clever. Yeah, I was wondering if it was the simple fact that setting the "disable" property is what was *actually* preventing the form submission. I'll give it a try ...
xanadont
@Calgary This really looks like a bug in MVC / ASP.Net Ajax. See my edits. Perhaps you don't need your work-around after all?
xanadont
@xanadont - Not MVC in my case... but definitely worth knowing.
Calgary Coder
A: 

Turns out it was a bug. See the edited question for the answer.

xanadont