Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable submit only if valid

im using jquery with asp.net mvc. Im doing something like this so that the submit button becomes disabled when clicked.

but if there are validation errors i dont want it to be disabled.

 $('form').submit(function () {
        if ($('form').valid()) {
            $('input[type=submit]', this).attr('disabled', 'disabled');
        }
    });

this makes it disabled, but even if there are validation errors. whats wrong?

like image 893
raklos Avatar asked Feb 28 '11 21:02

raklos


1 Answers

EDIT: i'll try again after my first screw up :P

$('form').submit(function () {
        if ($('form').valid()) {
            $('input[type=submit]', this).attr('disabled', 'disabled');
        }
    });

Maybe there are other forms on the page, and it's validating the wrong one?

$('form').submit(function () {
        if ($(this).valid()) {
            $(this).find('input[type=submit]').attr('disabled', 'disabled');
        }
    });

oh and did you $('form').validate() to enable validation (which I would guess is called automatically by MVC if you're using MVC's validation, but I'm not 100% sure about that)?

like image 160
Linkgoron Avatar answered Nov 07 '22 02:11

Linkgoron