views:

306

answers:

3

Hello everyone,

I want to disable the submit button when a user submits the form so that he may not click the submit button twice.

So I coded the following javascript in my page


$(document).ready(function(){
    $("form").submit(function(){
       $("form").find('input[type=submit]').attr('disabled', 'disabled');
    });
})

This works great. But when I applied the jquery validate library and appended the following code


$(document).ready(function(){
    $("form").submit(function(){
       $("form").find('input[type=submit]').attr('disabled', 'disabled');
    });

    $("form").validate({
            errorClass: "jqueryError",
            errorElement: 'label',
            success: 'jqueryValid',
            messages: {
                TopicCategory: {
                    required: 'Please choose a category for topic.'
                },
                TopicSubcategoryId: {
                    required: 'Please choose a subcategory for topic.'
                },
                TopicTitle: {
                    required: 'Please enter a title for topic.'
                }
            }
        });
})

The submit button got disabled after submitting form even if there are incomplete inputs in the website (i.e. form with errors and user is asked to fill them properly).
and when the user completes the form correcting all errors the submit button is disabled.

How do I first check if there are no errors in the form and then disable the submit button and then submit it finally.

Thanks

A: 

I have not started with jQuery yet. But I think, you can do something like this:

$(document).ready(function(){
    $("form").submit(function(){
    });

    $("form").validate({
            errorClass: "jqueryError",
            errorElement: 'label',
            success: 'jqueryValid',
            messages: {
                TopicCategory: {
                    required: 'Please choose a category for topic.'
                },
                TopicSubcategoryId: {
                    required: 'Please choose a subcategory for topic.'
                },
                TopicTitle: {
                    required: 'Please enter a title for topic.'
                }
            }
       $("form").find('input[type=submit]').attr('disabled', 'disabled');

        });
})
Kangkan
This is invalid syntax and will not work at all.
SLaks
You inserted a statement into a Json object declaration, that will break validate method, be careful :)
mamoo
:-) thanks for sparing your time in trying to solve my problem but that will never work.
Gaurav Sharma
Thanks @SLaks, @mamoo and @Gaurav. I learned one way of not doing something in jQuery now. I am planning to learn it. Just need to start. I have put this question in my favs. And a definite +1 for the question also.
Kangkan
+5  A: 

Add the following parameter to jQuery Validate:

submitHandler: function(form) {
    $(':submit', form).attr('disabled', 'disabled');
    form.submit();
}
SLaks
Thanks, that worked flawlessly. Can you explain the second line of code please.Is it equal to $("form").find("input[type=submit]").attr("disabled","disabled"); ?which one is faster ?
Gaurav Sharma
Mine is probably a little bit faster. I use [the `:submit` selector](http://api.jquery.com/submit-selector/), and the variant of the `$` function that takes an element to search in.
SLaks
thanks again for providing the link and a quick respone :-)
Gaurav Sharma
Thanks @SLaks. Now I too know some bit of jQuery from your answer. +1 from my side.
Kangkan
+1  A: 

I have not used the jquery validator plugin but reading the docs here

http://docs.jquery.com/Plugins/Validation/validate

you could

1) disable the button in the submitHandler callback

2) leave your existing disable code, but then re-enable the button in the invalidHandler callback

house9