admin管理员组

文章数量:1287840

I have two forms on our site #footer_leads and #footer_leads2 and i have a live submit event but need to verify a few things before the form gets summitted so i have this code

$('#footer_leads2, #footer_leads').live('submit', function(e){
    e.preventDefault();
    console.log('again');                           
    var form = $(this);  //save reference to form
    if(somevalidation){
        form.die();
        form.submit(); //submit form
    }

I assumed the jQuery die event would do the trick like in the above example but the page just into an infinite loop and crashes my browser....any ideas on how to do this

I have two forms on our site #footer_leads and #footer_leads2 and i have a live submit event but need to verify a few things before the form gets summitted so i have this code

$('#footer_leads2, #footer_leads').live('submit', function(e){
    e.preventDefault();
    console.log('again');                           
    var form = $(this);  //save reference to form
    if(somevalidation){
        form.die();
        form.submit(); //submit form
    }

I assumed the jQuery die event would do the trick like in the above example but the page just into an infinite loop and crashes my browser....any ideas on how to do this

Share Improve this question asked Oct 20, 2011 at 19:00 Matt ElhotibyMatt Elhotiby 44.1k91 gold badges224 silver badges328 bronze badges 1
  • yes but the .die or unbind should remove this handler – Matt Elhotiby Commented Oct 20, 2011 at 19:04
Add a ment  | 

5 Answers 5

Reset to default 5

From the jQuery die() page:

Note: In order for .die() to function correctly, the selector used with it must match exactly the selector initially used with .live().

Try $('#footer_leads2, #footer_leads').die() instead.

You shouldn't need to remove the handler and resubmit. Just wait on the preventDefault() until you know whether or not the form passed validation:

$('#footer_leads2, #footer_leads').live('submit', function(e) {
  if (!somevalidation)
    e.preventDefault();

  // Else, the form will continue submitting unimpeded.
});

Are you want to disable the form after the submit?

if yes, try this: $('input[type=submit]', this).attr('disabled', 'disabled');

I hope its help,

Don't unbind/die.

Just submit the form with the native method.

$('#footer_leads2, #footer_leads').live('submit', function() {

    if (true /* validation succeeded */ ) {
        this.submit(); //submit form, but don't use jQuery's method
    } else {
        return false;
    }
});

UPDATE:

Since it sounds like you're making an AJAX call for the validation, you could just do the this.submit() in the success: callback.

$('#footer_leads2, #footer_leads').live('submit', function() {

    $.ajax({
        url: 'some/path',
        context: this,  // set the context of the callbacks to the element
        ...
        success: function( d ) {
            if( d.is_valid ) {
                this.submit();
            } else {
                // give some feedback to the user for validation failure
            }
        }
    });

    return false; // block the submit by default
});

From the docs ( http://api.jquery./die/ ) :

Note: In order for .die() to function correctly, the selector used with it must match exactly the selector initially used with .live().

You need to use .unbind('submit')

本文标签: javascriptHow to remove a live submit event in jQueryStack Overflow