Giter VIP home page Giter VIP logo

Comments (8)

danielkummer avatar danielkummer commented on June 2, 2024 3

@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.

marioizquierdo avatar marioizquierdo commented on June 2, 2024

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.

ruiquelhas avatar ruiquelhas commented on June 2, 2024

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.

marioizquierdo avatar marioizquierdo commented on June 2, 2024

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.

ruiquelhas avatar ruiquelhas commented on June 2, 2024

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.

marioizquierdo avatar marioizquierdo commented on June 2, 2024

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.

ruiquelhas avatar ruiquelhas commented on June 2, 2024

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.

brock avatar brock commented on June 2, 2024

@marioizquierdo +1

from jquery.serializejson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.