Search code examples
jqueryformssubmitbindlive

Trying to submit a form with live(), delegate and bind()


Incase a form is not valid, I want to print a message in the browser console.

Folling code with bind works:

jQuery(document).ready(function () {
    $('form').bind('submit', function () {
        if ($j(this).valid() == false) {
            console.log("Warning: form not valid");
        };
    });
});

and works as well when I replace bind by submit:

$('form').submit(function () {

But doesnt work when I use delegate:

$('body').delegate('form', 'submit', function () {

And even with live no success:

$('form').live('submit', function () {

Form is not dynamicly insertet. What could be the reason for that live and delegate don't work??

EDIT

I just found out that jquery.validate.min.js was causing the problem. Hm, but how can I prevent my own form events to be prevented?


Solution

  • You will have another handler either on the form element or an ancestor between the form and the body which is preventing the propagation of the event, either by;

    1. Calling stopPropagation() on the event object
    2. Calling stopImmediatePropagation() on the event object
    3. return false'ing from the event handler.

    You need to find that handler, and stop it doing so. Bear in mind this may be the work of a plugin you have enabled as well.