jQuery Validate plugin, enable submit button when form is valid

前端 未结 4 840
感动是毒
感动是毒 2020-12-15 11:50

I have an enabled and disabled state for the submit button on my form.

The conditions are as follows:

If all input fields have been entered and are

4条回答
  •  不知归路
    2020-12-15 12:02

    You would simply construct a blur (or even a keyup) handler function to toggle the button based on the form's validity. Use the plugin's .valid() method to test the form.

    $('input').on('blur', function() {
        if ($("#myform").valid()) {
            $('#submit').prop('disabled', false);  
        } else {
            $('#submit').prop('disabled', 'disabled');
        }
    });
    

    DEMO: http://jsfiddle.net/sd88wucL/


    Instead, you could also use both events to trigger the same handler function...

    $('input').on('blur keyup', function() {
        if ($("#myform").valid()) {
            $('#submit').prop('disabled', false);  
        } else {
            $('#submit').prop('disabled', 'disabled');
        }
    });
    

    DEMO 2: http://jsfiddle.net/sd88wucL/1/

    Source: https://stackoverflow.com/a/21956309/594235

提交回复
热议问题