JQuery re-bind.submit () after an AJAX call

I am creating a forum style system in which posts are created and users can post comments on the original post. (What I do is actually very different in context, but the theme is the same).

The comment form is part of the message that is pulled using jQuery $ .get ().

Currently, the dispatch handler:

$('#add-stage-form').submit(function(){

        var data = $('#add-stage-form').serialize();
        add_stage(data);
        return false;   
    });

Good. Now I need to be tied again when it is being pulled in, in the same case it could be used live().

And please, if anyone suggests live('submit', function(){ ... })I WILL scream, because submit doesn't work like a click!

I also know well that you could bind the 'click' event to the same button - I would much more likely bind the .submit () event, if possible

Thanks in advance!:)

+3
source share
2 answers

You can add the binding manually in the onComplete callback of the get method ...

$.get('getposts.html', function(data) {
    var myForm = $(data);
    $('.formContainer').append(myForm);

    $('.add-stage-form', myForm).submit(function(){

        var data = $(this).serialize();
        add_stage(data);
        return false;   
    });

});

* edit Changed add-stage-form as a class, not id. Did you mention what the message form will be?

+5
source

Have you tried using Live()? :)

But seriously, it might not be such a bad idea if you attach a click event to a button that then triggers an operation jQuery.Post(). Or even a javascript method that sends the page.

, , .

, .

+1

All Articles