Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Form plugin - how to make ajaxForm() "live"?

So I'm turning an "edit" form into an ajaxForm with the following:

$('#reviewForm').ajaxForm({
    success: function (response) {
        $('#bookReview').html(response);
    }
});

This returns the same form, that can be edited again, if necessary. The second form submission, however, no longer has the ajaxForm() attached to it, which makes sense.

How do I make sure this form is always an ajaxForm, no matter how many submissions have taken place, similar to how the live() function works?

like image 273
asfsadf Avatar asked Sep 26 '10 03:09

asfsadf


2 Answers

$('#myFormId').live('submit', function() {
    // submit the form
    $(this).ajaxSubmit();
    // return false to prevent normal browser submit and page navigation
    return false;
});

Sligtly modified example for ajaxSubmit from http://jquery.malsup.com/form/#api.

like image 171
Slotos Avatar answered Oct 04 '22 11:10

Slotos


From the documentation :

delegation

true to enable support for event delegation requires jQuery v1.7+

// prepare all existing and future forms for ajax submission
$('form').ajaxForm({
     delegation: true
});
like image 21
younes0 Avatar answered Oct 04 '22 13:10

younes0