jQuery disable button (NOT Submit) until field validates + Validation Plugin

前端 未结 4 1559
孤街浪徒
孤街浪徒 2020-12-02 19:55

I have 4 buttons on my form, a Submit, Cancel, Add and Remove button. All are working as expected but I would like to disable the Add button until the input field validates.

4条回答
  •  没有蜡笔的小新
    2020-12-02 20:07

    I've used the validation plugin before but I can't remember all the details. I'm pretty sure it's something like this:

    $(function() {
        $('#myForm').validate(
            rules: {
                'elementToWatch': {
                    success: function(element) { 
                        if($("#myform").validate().element("#myselect")) {
                            $('#chargeamtaddButton:disabled').removeAttr('disabled');
                        } else {
                            $('#chargeamtaddButton').attr('disabled','disabled');
                        }
                    },
                    // etc...
                 },
                 // etc...
             },
             // etc...
         });
     });
    

    Since there are so many ways to use the validation plugin, it's hard to give you perfect advice but I hope what I've shown gives you a better idea of how to move forward. Maybe if you gave some more code and HTML, I could help you out more.

提交回复
热议问题