Atif
Atif

Reputation: 10880

Serialize a Form but some Elements

I have a form which has elements in a couple of divs.

Based on a drop down, some of these divs might be hidden and some will be displayed.

i want to Serialize the form with just the element which are visible and not the hidden divs.

Is there any way I can filter out those hidden divs.

tried this but doesn't work

$('<tr />').data($(this).find('form:not(.child:hidden)').serializeObject());

serializeObject is a plugin which converts serializeArray into an Object.

Upvotes: 0

Views: 1460

Answers (2)

Tieson T.
Tieson T.

Reputation: 21191

If you disable the unused form elements, they do not get submitted with the form. That may be the easiest/quickest solution.

From a previous question on SO, I have these defined in my local extensions:

/* 
    Extension methods to quickly enable/disable form elements 
    @@see http://stackoverflow.com/questions/625109/jquery-script-load-timing/625126#625126
*/
$.fn.disable = function() {
    return this.each(function() {
        if (typeof this.disabled != "undefined") this.disabled = true;
    });
}

$.fn.enable = function() {
    return this.each(function() {
        if (typeof this.disabled != "undefined") this.disabled = false;
    });
}

So, you could do something like:

$('.hidden-div-class').disable();

And then serialize the form.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Assuming that in your context this is the <form> try the following:

var data = $(':input:visible', this).serialize();
$('<tr />').data(data);

And here's a live demo.

Upvotes: 5

Related Questions