Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax - Prevent double click on submit

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"));
}
like image 613
Valamas Avatar asked Jun 22 '11 04:06

Valamas


People also ask

How to prevent double click event on submit button?

on('dblclick', function (event) { event. preventDefault(); }); });

How can stop double click on submit button in jQuery?

Javascript Codeattr('disabled', true); }); }); As you can see above we have "$this. attr('disabled', true)". This function will help you to disabled the button when clicking but take not after we clicked the button will remain disabled.

How to avoid double click on submit button in php?

click(function(){ $(this). prop('disabled', true); }); this solve the issue.

How to prevent multiple clicks on submit button in JavaScript?

In practice this can cause a form to be submitted, or some other event triggered, more than once. The second button however will only accept a single click and ignore all subsequent clicks. The trick is to use JavaScript to set the disabled property of the button to true.


1 Answers

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.

like image 53
Darin Dimitrov Avatar answered Oct 12 '22 08:10

Darin Dimitrov