ajax - Prevent double click on submit

﹥>﹥吖頭↗ 提交于 2019-11-28 07:25:29

问题


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"));
}

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6434840/ajax-prevent-double-click-on-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!