Hello
How can I force .serialize() function not to read the empty fields? I only want it to serialize those fields with data inside of them…
Thank you…
Hello
How can I force .serialize() function not to read the empty fields? I only want it to serialize those fields with data inside of them…
Thank you…
An empty field is still considered to be data.
There are two options. Manually pre-process the data destined to be serialized and remove the empty fields yourself, with the delete method, or improve the smarts of what’s going to be reading the serialized data.
I’d go with the second option myself.
or improve the smarts of what’s going to be reading the serialized data.
Example?
What’s going to be reading the serialized data?
I would serialize the data first and then remove empty parameters, an example:
var serializedData = $('form').serialize();
// serializedData = 'name=&age=99&blah=';
serializedData = serializedData.replace(/&?[^=]+=&|&[^=]+=$/g,'');
// serializedData = 'age=99' <- Removes all empty values
Now that’s a nice solution.
Thanks Paul!
Great, thank you guys…
I’m afraid this is removing values also
i had
&att=1&att2=2&att3=&att4=&att5=
I got
&att=1&att2=att5=
Sorry, try this:
serializedData.replace(/&?[^=&]+=(&|$)/g,'');
A breakdown of the regular expression:
/
&? # Optional ampersand
[^=&]+ # 1 or more characters except = or &
= # Equals sign
(&|$) # Either another ampersand or end of string/line
/g # Global search
Thank you, I will check it…
I hate regular expressions anyway
This is also causing an problem…
if i have let’s say 10 fields, something like
att1= text
att2= text
att3=text
att4=
att5=text
att6=
att7=
att8=text
att9=text
att10=text
Some are filled and others arent…
I will get something like
att1=text&att2=text&att3=textatt5=textatt8=text&att9=text&att10=text
Check the bold section…