0

I have a data entry form as razor view in my MVC application. This view contains, some input controls, validators, and a submit button. when a user hits Submit button, it performs validations, and if any of them gets failed the user submit method gets terminated. When all validations are satisfied, the form gets submitted, but it takes a little more amount of time as some business logic gets executed. Now I do not want users to allow hitting submit button during this. And so Submit button should be disabled, and it should be re-enabled once the submit is completed.

How can I achieve this? I think I need to write JQuery for this, but not sure, which event to handle.

2 Answers 2

5

Without seeing the HTML... or the javascript...

$('form').submit(function(){
    var $submit = $(this).find('input[type=submit]').prop('disabled', true);

    $.ajax({
        url: 'foo',
        //... the rest...

        complete: function(){
            $submit.prop('disabled', false);
        }
    });
})
Sign up to request clarification or add additional context in comments.

1 Comment

This is a better practice than the accepted answer.
1

Try the following:

$("form").submit(function() {
    var form = this;
    setTimeout(function() {
       $(':submit', form).attr('disabled', true);
    }, 50)
});

1 Comment

And if it takes two seconds\minutes... hardcode time is problematic most of the times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.