tags:

views:

37

answers:

1

I have some form containers with a form in each,

form.submit function like this:

$('.form-container form').submit(function(){
    $.post(url,data,callback);
});

callback function:

function callback(data){
    container.html(data);
}

Question is, how can I get the container, are there any way that I can pass it from submit function?

Because the container is danymic, I cann't simply get it by id or so.

Thanks!!

A: 

I don't know what a "container" is in this sense, but if it's available in your submit function, then you can declare it in the submit function's local scope, and use it in the callback, either like so:

$('.form-container form').submit(function() {
    var container = ...; // your container
    $.post(url, data, function(data) {
        container.html(data);
    });
});

or if you prefer to keep the callback separate:

$('.form-container form').submit(function() {
    var container = ...;
    $.post(url, data, function(data) { callback(container, data); });
});

function callback(container, data) {
    container.html(data);
};
David Hedlund
Thanks David, I just reallized that.I declare container in the submit function and it's OK now.
Dong