JQuery: binding .validate plugin to .live

I need to bind a jQuery validate plugin to the forms that I generate on the fly. As far as I know, the .live()only way to do this in jQuery. This does not seem to work, at least not as I try. I think this is normal and it is just eebkac!

This works fine:

$("form#form_CreateUser").validate({  
   submitHandler: function(form) {  
    // do other stuff for a valid form  
    UserSubmit();  
   }  
});  

This generates syntax errors:

$("form#form_CreateUser").live('validate', function() {  
   submitHandler: function(form) {  
    // do other stuff for a valid form  
    UserSubmit(); 
   }  
});  
+3
source share
1 answer

This does not work because you misunderstood how it works validate. This is similar to event handler functions such as click, but in fact it is not.

click bind('click', fn). validate bind. live bind , , validate.

- , validate , . , AJAX, ajaxComplete:

function setupValidate() {
    $("form#form_CreateUser").validate({
        submitHandler: function(form) {
            // do other stuff for a valid form
            UserSubmit();
        }
    });
}

$(document).ajaxComplete(setupValidate); // after every AJAX request
setupValidate(); // right now
+14

Source: https://habr.com/ru/post/1789053/


All Articles