instance method Form#serialize

View source on GitHub →

Form#serialize([options]) → String | Object
  • options (Object) – A list of options that affect the return value of the method.

Serializes form data to a string suitable for Ajax requests (default behavior) or, if the hash option evaluates to true, an object hash where keys are form control names and values are data.

Depending of whether or not the hash option evaluates to true, the result is either an object of the form {name: "johnny", color: "blue"} or a String of the form "name=johnny&color=blue", suitable for parameters in an Ajax request. This method mimics the way browsers serialize forms natively so that form data can be sent without refreshing the page.

See Form.serializeElements for more details on the options.

// -> 'username=sulien&age=22&hobbies=coding&hobbies=hiking'
// -> {username: 'sulien', age: '22', hobbies: ['coding', 'hiking']}

Disabled form elements are not serialized (as per W3C HTML recommendation). Also, file inputs are skipped as they cannot be serialized and sent using only JavaScript.

This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.