views:

72

answers:

2

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?

+1  A: 

You can either include the ajaxForm call in the response like:

<!-- the html for the form -->
<script type="text/javascript">
  $('#reviewForm').ajaxForm();
</script>

or you could do it as part of the success function:

function ajaxify(response, status, xhr, form){
   var review = $('#bookReview').html(response);
   $('form#reviewForm', review).ajaxForm({
      'success': ajaxify
   });
}

$('#reviewForm').ajaxForm({
   'success': ajaxify
});

I recommend the latter.

prodigitalson
Hmmm, with that second one, isn't it missing some info. on the end? It's like a never-ending redundancy, isn't it?
PolishedTurd
Oh i see what youre saying... well you could just make it another function ... ill update
prodigitalson
My brain is in knots. Where do the 4 parameters come from? Javascript is totally foreign to me. Thanks.
PolishedTurd
those paramters are always passed to the succes function for `ajaxForm`... see the docs: http://jquery.malsup.com/form/#options-object
prodigitalson
Getting an error under the second "var": "Expected identifier or string"
PolishedTurd
yeah thats cause im an idiot :-) corrected.
prodigitalson
Uh oh, if you're an idiot, what does that make me? :-oThanks for all the help. Any recommendations on how to learn this frustrating client-side junk?
PolishedTurd
i honestly dont remember how i picked up JS... bu tif i had to do it again and was starting from scratch i would find some resources on core js, specifically scope evaluation, the DOM, anonymous objects, closures, etc.
prodigitalson
I've got the old Rhino book, that I haven't looked at in ages. Maybe I'll dust that one off. Thanks.
PolishedTurd
+1  A: 
$('#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.

Slotos