Comments (8)
@marioizquierdo solution doesn't work for me if I'm using array elements, but I found this solution quite satisfying:
obj = $form.find(':input').filter(function () {
return $.trim(this.value).length > 0
}).serializeJSON();
from jquery.serializejson.
The method serializeJSON works on any set of jQuery elements.
You could filter your jQuery object to keep only what you want to serialize, i.e.:
$someForm.not('[value=""]').serializeJSON();
And there's also the :skip
type, that could be used on input names in order to filter them out (feature added in version 2.4.0). For example:
<input type="text" name="you[are]" value="in"/>
<input type="text" name="I[am]:skip" value="out"/>
$('input').serializeJSON();
// returns =>
{ "you": {"are": "in"}} // Note that {"I": {"am": "out"}} is not included
Does this work for you?
from jquery.serializejson.
The first alternative would fit my use-case, but I tried it, and it does not seem to be working as I expected, the empty fields are still added to the object as empty strings. I've also added a test for it, and it fails.
The second one does not fit my use-case, because I just want fields to be optional, not skipped entirely, in order to avoid sending dummy data through the wire.
from jquery.serializejson.
There must be something wrong with your jQuery object.
Make sure you have selected only the fields you want because serializeJSON acts only on those elements.
I've checked StackOverflow and this seems to be a good answer:
http://stackoverflow.com/questions/608730/how-do-i-use-jquerys-form-serialize-but-exclude-empty-fields
$("#myForm :input[value!='']").serializeJSON()
from jquery.serializejson.
I've added the following test case to your suite.
describe('options', function () {
beforeEach(function () {
// ...
});
// ...
describe('filters empty values using proper selector', function () {
it('does not return any value', function () {
obj = $form.not('[value=""]').serializeJSON();
expect(obj).toEqual({
"Numeric 0": "0",
"Numeric 1": "1",
"Numeric 2.2": "2.2",
"Numeric -2.25": "-2.25",
"Bool true": "true",
"Bool false": "false",
"Null": "null",
"String": "text is always string",
// I want the following to not be included in the result
// "Empty": ""
});
});
});
});
This fails. Am I missing something?
from jquery.serializejson.
I see. Well, it turns out that selecting the right inputs from jQuery is not that easy after all.
In this case, you can not apply .not('value=""')
to the form, because that a jQuery object with a single matched element (the <form>
), which obviously doesn't have an empty value.
What you want to do is to match the inputs with no empty value, for example:
obj = $form.find('input').not('[value=""]').serializeJSON();
// or
obj = $form.find(':input[value!=""]').serializeJSON();
Note that the selector would be somehow different if you also had textareas or select tags.
This is a general jQuery problem, but I've seen others having the same issue. Maybe an option that is able to filter them out would help. I probably implement that in a future release. But for now, just make sure you are selecting the fields that you want to serialize.
from jquery.serializejson.
Yes, it works that way, thanks!
I'm not a big user of jQuery, so I thought find
and the general $
selector would have the same behaviour, but apparently not.
Anyway, I've actually implemented that option in a fork, in case you fancy a pull request.
from jquery.serializejson.
from jquery.serializejson.
Related Issues (20)
- checkboxUncheckedValue not working for me HOT 1
- Option for include parsed 0 values HOT 1
- Raise ERROR: Invalid type in value HOT 5
- Mixup between colon and semicolon HOT 2
- .serializeJSON returns undefined HOT 4
- Provide dom element into custom type function HOT 5
- Is there something wrong about array? HOT 1
- Make an es6 module? HOT 1
- Ability to use attributes other than name for building the json HOT 3
- Consider passing element name to custom type function. HOT 4
- Versioning scheme? HOT 1
- Empty SELECT multiple HOT 3
- AMAZING, THANK YOU
- It doesn't serialize type=file HOT 3
- Initialize form values from object HOT 3
- Radio Buttons are not working in >3.2.0 HOT 2
- Doesn't work with ASP.NET MVC naming conventions HOT 5
- Checking which input has the error? HOT 2
- checkboxCheckedValue option HOT 1
- Disabled elements are ignored? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jquery.serializejson.