Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing functions for serializing form data #9

Open
itsrachelfish opened this issue Jun 9, 2015 · 1 comment
Open

Missing functions for serializing form data #9

itsrachelfish opened this issue Jun 9, 2015 · 1 comment

Comments

@itsrachelfish
Copy link
Member

.serialize() should be a thing, but it shouldn't be like jQuery. Instead of providing .serialize() and .serializeArray(), the argument should determine what type of output is generated.

.serialize() - No argument, defaults to 'url'
.serialize('url') - String of URL encoded data
.serialize('array') - Array of key / value pairs
.serialize('object') - Object with named properties

@itsrachelfish
Copy link
Member Author

Something like this might be helpful:

        var data = {};

        $(form).find('input, select').each(function()
        {
            if($(this).attr('type') == "submit")
            {
                return;
            }

            var name = $(this).attr('name');
            var value = $(this).value();

            if($(this).attr('type') == 'checkbox')
            {
                if($(this).prop('checked'))
                {
                    value = true;
                }
                else
                {
                    value = false;
                }
            }

            // If this input name has already been used
            if(data[name] !== undefined)
            {
                // Is it an array? If not, convert it
                if(!Array.isArray(data[name]))
                {
                    data[name] = [data[name]];
                }

                // Push value to input array
                data[name].push(value);
            }
            else
            {
                // Otherwise, save the value by itself
                data[name] = value;
            }
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant