ajax - Prevent double click on submit
Asked Answered
L

2

11

How can I prevent the user from double clicking submit button on my signup form which is an ajax partial view?

I regret to ask since this would have already been asked. I just can't find a clear working answer now matter where I search. Disabling the button prevent submit. Using a var javascript clickcount+alert+return_false does not reset.

Environment: asp.net mvc3

View: Form displays onload: @RenderPage("_SignupForm.cshtml")

Submission using:

@using (Ajax.BeginForm("Index", "Signup", null,
     new AjaxOptions
                {
                    UpdateTargetId = "signupForm", 
                    InsertionMode = InsertionMode.Replace, 
                    HttpMethod = "POST",
                    LoadingElementId="progress"
                }
     ))

Submit control: <input type="submit" value="Sign up" />

SignupController :

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(SignupModel formvalues)
{
    Thread.Sleep(5000);

    string errors = "";
    if (TryValidateModel(formvalues))
    {
        errors = SignupAPI.Signup(formvalues); //includes custom validation
    }

    if (ModelState.IsValid == false || string.IsNullOrEmpty(errors) == false)
    {
        ViewBag.Errors = errors;
        return PartialView("_SignupForm", formvalues);
    }
    else
        return Redirect(string.Concat("http://localhost/welcome"));
}
Legalize answered 22/6, 2011 at 4:18 Comment(0)
A
19

Try with the following script:

$('form').submit(function () {
    if ($(this).valid()) {
        $(':submit', this).attr('disabled', 'disabled');
    }
});

Make sure you execute it also in the success callback of your AJAX request in order to reattach the submit event when the form is replaced with a new content in the DOM, or the second time it might no longer work.

Arthritis answered 22/6, 2011 at 5:58 Comment(2)
This works great except if you're listening to $("form").on("invalid-form.validate" to capture invalid form submissions, the call to .valid() will cause the listener to fire twice.Albany
This works great except if you're listening to $("form").on("invalid-form.validate" to capture invalid form submissions, the call to .valid() will cause the listener to fire twice.Albany
K
0

UPDATE: submission was not working because onclick was not returning true

<input type="submit" value="Sign Up" onclick="this.disabled = true; return true;"/>

this will disable the button and the second click on the button won't work

Kail answered 22/6, 2011 at 4:29 Comment(7)
@Arturo: The form fails to submit at all now.Legalize
@Arturo: The form submits, however, for some odd reason, the returned page is only the view and not the surrounding layout. I suspect the ajax call is being circumvented. I have a Thread.Sleep(5000) and I do not see the ajax indicator while waiting for the postback.Legalize
can you post your form element once is being rendered in your browser?Kail
@Arturo <form action="/Signup" data-ajax="true" data-ajax-loading="#progress" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#signupForm" id="form0" method="post">. Before, i had changed the command to form0.submit()Legalize
@Arturo: thanks for trying to help. I tried the new disableElement function and the result is that the form does not post.Legalize
try with the second option, just the submit input <input type="submit" value="Sign Up" onclick="this.disabled = true; return true;"/> the problem is that the onclick was not returning trueKail
@Arturo: same result. Button disables and form does not submit.Legalize

© 2022 - 2024 — McMap. All rights reserved.