Code Monkey home page Code Monkey logo

jquery-serializeform's Introduction

serializeForm

Make an object out of form elements. This fork adds the checkboxBoolean option described below.

Getting Started

Download the production version or the development version.

To Use

$.fn.serializeForm makes an object out of form elements inside of the specified item.

Example:

<div id="test">
  <input name="text1" value="txt-one" />
  <input type="checkbox" name="top[child][]" value="1" checked="checked" />
  <input type="checkbox" name="top[child][]" value="2" checked="checked" />
  <input type="checkbox" name="top[child][]" value="3" checked="checked" />

  <select name="another[select]">
    <option value="opt"></option>
  </select>
</div>
$( '#test' ).serializeForm();

Returns

{ text1: "txt-one",
  top: {
    child: [ "1", "2", "3" ]
  },
  another: {
    select: "opt"
  }
}

###checkboxBoolean option

Checkboxes are often used for boolean values such as toggling options on and off. In these cases, you want all of that data sent to the server, including checkboxes that aren't checked. By default this is not how standard HTML forms work, here is a good explanation of their intended behavior.

Either your back end knows exactly what is supposed to be in the form data and handles missing values as false or you can use two radio buttons for every boolean value you want in the form. The former limits your flexibility and the latter is usually worse UI plus it will send text values that you have to parse into booleans.

With the checkboxBoolean option, you can return all checkbox values as either the numbers 1 or 0, or the booleans true or false.

Example:

<div id="test">
  <input type="checkbox" name="top[child][]" checked="checked" />
  <input type="checkbox" name="top[child][]" checked="checked" />
  <input type="checkbox" name="top[child][]" />
</div>
$( '#test' ).serializeForm({
    checkboxBoolean: true
});

Returns

{
  top: {
    child: [ true, true, false ]
  }
}

Or with numbers:

$( '#test' ).serializeForm({
    checkboxBoolean: 1
});

Returns

{
  top: {
    child: [ 1, 1, 0 ]
  }
}

If you set a value on a checkbox, it will function like a normal checkbox and return that value if it is checked:

<div id="test">
  <input type="checkbox" name="top[child][]" value="hello" checked="checked" />
  <input type="checkbox" name="top[child][]" value="hello again" /> <!-- not checked -->
  <input type="checkbox" name="top[child][]" checked="checked" />
  <input type="checkbox" name="top[child][]" />
</div>
$( '#test' ).serializeForm({
    checkboxBoolean: true
});

Returns

{
  top: {
    child: [ 'hello', true, false ]
  }
}

Release History

1.1.3 Fix multiwork keyword in jquery manifest file 1.1.2 Update manifest files and erase old tags for plugin site 1.1.1 Rename to serializeForm from serializeObject 1.1.0 Support finding input elements on the jQuery collection itself and find in multiple elements 1.0.0 Actual release while moving to grunt and adding licensing information

License

Copyright (c) 2012 Dan Heberden Licensed under the MIT, GPL licenses.

Important notes

Please don't edit files in the dist subdirectory as they are generated via grunt. You'll find source code in the src subdirectory!

While grunt can run the included unit tests via PhantomJS, this shouldn't be considered a substitute for the real thing. Please be sure to test the test/*.html unit test file(s) in actual browsers.

Installing grunt

This assumes you have node.js and npm installed already.

  1. Test that grunt is installed globally by running grunt --version at the command-line.
  2. If grunt isn't installed globally, run npm install -g grunt to install the latest version. You may need to run sudo npm install -g grunt.
  3. From the root directory of this project, run npm install to install the project's dependencies.

Installing PhantomJS

In order for the qunit task to work properly, PhantomJS must be installed and in the system PATH (if you can run "phantomjs" at the command line, this task should work).

Unfortunately, PhantomJS cannot be installed automatically via npm or grunt, so you need to install it yourself. There are a number of ways to install PhantomJS.

Note that the phantomjs executable needs to be in the system PATH for grunt to see it.

jquery-serializeform's People

Contributors

danheberden avatar rgrwkmn avatar rwaldron avatar silex avatar

Watchers

 avatar

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.