Excluding certain inputs on serialize

Solution 1:

You don't need the :, because input is an element not a pseudo selector. Secondly you cannot use an object and a text string like that in your selector. You instead need to supply this as the scope argument to $():

$('#ofform').live('submit', function(e) {
    e.preventDefault();
    var serializedReturn = $('input[name!=security]', this).serialize();        
});

Solution 2:

First, you need to invoke the .find() method like:

var serializedReturn = $(this).find('input[name!=security]').serialize(); 

Otherwise the complete string would go into the css query engine (Sizzle).

Secondly:

I have another form with the id of ofform-reset and if i use:

You need to change this. It's invalid markup to have multiple ID's. If I understood you wrong here, the first solution might also help you here, invoking the .find() method aswell:

var serializedReturn = $('#ofform').find('input[name!=security]').serialize(); 

Solution 3:

I am not happy with 'input[name!=security]' because it exclude all other input type like select,.. You can add them manually but this list just keep increasing with new HTML tags. So with every new tag coming, your code is broken again.

Here is my solution:

$form.find(':not(input[name=draft])').serialize()

or

$('form[name=expenses] :not(input[name=draft])').serialize()

Space is very important in second example.

Solution 4:

In my situation, this worked fine:

$("fieldset").not(".tasks-container").serialize()

Solution 5:

Solved! You should use the given solution to exclude input field to be serialized. It's tested and solved my problem.

var formdata = $($("#myform")[0].elements).not("#field_id").serialize();

for multiple fields, you can use the class name to exclude them.

var formdata = $($("#myform")[0].elements).not(".class_name").serialize();