Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding certain inputs on serialize

I am trying to exclude an input by name (it is a hidden input holding my nonce)

The following question is almost what I am looking for:

How do I use jQuery's form.serialize but exclude empty fields

but I have 2 questions about the solution there- which states that to serialize form data except for empty inputs and inputs where the value = "."

$("#myForm :input[value][value!='.']").serialize();

first of all, i can't get it to work with the jquery variable "this"

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

And secondly I have a seperate form with the id of ofform-reset and if i use:

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

it picks up the inputs in the other #ofform-reset form, AND/OR inputs that aren't enclosed within a tag.

found the answer in one of my previous questions. invalid markup of the style:

<form id="ofform">
 <div id="toolbar">
 <button id="save">Save</button>
</form>
<form id="ofform-reset">
 <button id="reset">Reset</button>
</form>
</div>

now to figure out how to use 2 different buttons to control the same form

like image 696
helgatheviking Avatar asked Dec 29 '10 17:12

helgatheviking


2 Answers

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();         }); 
like image 140
prodigitalson Avatar answered Sep 20 '22 11:09

prodigitalson


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(); 
like image 43
jAndy Avatar answered Sep 22 '22 11:09

jAndy