namespace Form
Description
Utilities for dealing with forms in the DOM.
Form is a namespace for all things form-related, packed with form
manipulation and serialization goodness. While it holds methods dealing
with forms as a whole, its submodule Form.Element deals with specific
form controls.
Many of these methods are also available directly on form elements.
Related utilities
Methods
Class methods
-
disable #
Form.disable(form) ⇒ Element
Form#disable() ⇒ ElementDisables the form as a whole. Form controls will be visible but uneditable.
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.
-
enable #
Form.enable(form) ⇒ Element
Form#enable() ⇒ ElementEnables a fully- or partially-disabled form.
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.
-
findFirstElement #
Form.findFirstElement(form) ⇒ Element
Form#findFirstElement() ⇒ ElementFinds the first non-hidden, non-disabled control within the form.
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.
-
focusFirstElement #
Form.focusFirstElement(form) ⇒ Element
Form#focusFirstElement() ⇒ ElementGives keyboard focus to the first element of the form. Returns the form.
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.
-
getElements #
Form.getElements(form) ⇒ [Element…]
Form#getElements() ⇒ [Element…]Returns a collection of all controls within a form.
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.
-
getInputs #
Form.getInputs(form [, type [, name]]) ⇒ [Element…]
Form#getInputs([type [, name]]) ⇒ [Element…]-
type(String) – A value for thetypeattribute against which to filter. -
name(String) – A value for thenameattribute against which to filter.
Returns a collection of all
INPUTelements in a form.Use optional
typeandnamearguments to restrict the search on these attributes.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.
-
-
request #
Form.request(form[, options]) ⇒ Ajax.Request
Form#request([options]) ⇒ Ajax.Request-
options(Object) – Options to pass along to theAjax.Requestconstructor.
A convenience method for serializing and submitting the form via an
Ajax.Requestto the URL of the form'sactionattribute.The
optionsparameter is passed to theAjax.Requestinstance, allowing one to override the HTTP method and/or specify additional parameters and callbacks.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.
-
-
reset #
Form.reset(form) ⇒ ElementResets a form to its default values.
-
serialize #
Form.serialize(form[, options]) ⇒ String | Object
Form#serialize([options]) ⇒ String | Object-
options(Object) – A list of options that affect the return value of the method.
Serialize form data to an object or string suitable for Ajax requests.
See
Form.serializeElementsfor details on the options.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.
-
-
serializeElements #
Form.serializeElements(elements[, options]) ⇒ String | Object-
elements(Array) – A collection of elements to include in the serialization. -
options(Object) – (Optional) A set of options that affect the return value of the method.
Serialize an array of form elements to an object or string suitable for
Ajaxrequests.As per the HTML spec, disabled fields are not included.
If multiple elements have the same name and we're returning an object, the value for that key in the object will be an array of the field values in the order they appeared on the array of elements.
The Options
The options allow you to control two things: What kind of return value you get (an object or a string), and whether and which
submitfields are included in that object or string.If you do not supply an
optionsobject at all, the options{ hash: false }are used.If you supply an
optionsobject, it may have the following options:hash(Boolean):trueto return a plain object with keys and values (not aHash; see below),falseto return a String in query string format. If you supply anoptionsobject with nohashmember,hashdefaults totrue. Note that this is not the same as leaving off theoptionsobject entirely (see above).submit(Boolean |String): In essence: If you omit this option the first submit button in the form is included; if you supplyfalse, no submit buttons are included; if you supply the name of a submit button, the first button with that name is included. Note that thefalsevalue must really befalse, not falsey; falsey-but-not-false is like omitting the option.
(Deprecated) If you pass in a Boolean instead of an object for
options, it is used as thehashoption and all other options are defaulted.A hash, not a Hash
If you opt to receive an object, it is a plain JavaScript object with keys and values, not a
Hash. All JavaScript objects are hashes in the lower-case sense of the word, which is why the option has that somewhat-confusing name. -