Skip to content

Missing functions for serializing form data #9

Open

Description

@itsrachelfish

.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

Activity

  1. itsrachelfish commented on Jan 12, 2017

    @itsrachelfish
    MemberAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions