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>
</d开发者_StackOverflow中文版iv>
now to figure out how to use 2 different buttons to control the same form
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();
});
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();
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.
In my situation, this worked fine:
$("fieldset").not(".tasks-container").serialize()
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();
You can filter result array
var ignoreFields = ["_csrf"];
$('form')
.serializeArray()
.filter(function(val){
return ignoreFields.indexOf(val.name) === -1;
});
Or shorter variant
$('form').serializeArray().filter(val => ignoreFields.indexOf(val.name) === -1)
try this
var data = $.param($.map($("#form_name").serializeArray(), function(v,i) {
return (v.name.includes("substring")) ? null : v;
}));
精彩评论