jQuery Form Plugin

Overview

The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX. The main methods, ajaxForm and ajaxSubmit, gather information from the form element to determine how to manage the submit process. Both of these methods support numerous options which allows you to have full control over how the data is submitted. Submitting a form with AJAX doesn’t get any easier than this!

Quick Start Guide

1Add a form to your page. Just a normal form, no special markup required:

  1. <form id=”myForm” action=”comment.php” method=”post”> 
  2.     Name: <input type=”text” name=”name” /> 
  3.     Comment: <textarea name=”comment”></textarea> 
  4.     <input type=”submit” value=”Submit Comment” /> 
  5. </form>

2Include jQuery and the Form Plugin external script files and a short script to initialize the form when the DOMis ready:

  1. <html> 
  2. <head> 
  3.     <script type=”text/javascript” src=”jquery-1.3.2.js”></script> 
  4.     <script type=”text/javascript” src=”jquery.form.js”></script> 
  5.  
  6.     <script type=”text/javascript”> 
  7.         // wait for the DOM to be loaded 
  8.         $(document).ready(function() { 
  9.             // bind ‘myForm’ and provide a simple callback function 
  10.             $(‘#myForm’).ajaxForm(function() { 
  11.                 alert(“Thank you for your comment!”); 
  12.             }); 
  13.         }); 
  14.     </script> 
  15. </head> 

That’s it!

When this form is submitted the name and comment fields will be posted to comment.php. If the server returns a success status then the user will see a “Thank you” message.

表单插件API提供了几个方法,让你轻松管理表单数据和进行表单提交。

ajaxForm

增 加所有需要的事件监听器,为AJAX提交表单做好准备。ajaxForm不能提交表单。在document的ready函数中,使用ajaxForm来为 AJAX提交表单进行准备。ajaxForm接受0个或1个参数。这个单个的参数既可以是一个回调函数,也可以是一个Options对象。
可链接(Chainable):可以。

实例:

$(‘#myFormId’).ajaxForm();

ajaxSubmit

马上由AJAX来提交表单。大多数情况下,都是调用ajaxSubmit来对用户提交表单进行响应。ajaxSubmit接受0个或1个参数。这个单个的参数既可以是一个回调函数,也可以是一个Options对象。
可链接(Chainable):可以。

实例:
// 绑定表单提交事件处理器
$(‘#myFormId’).submit(function() {
// 提交表单
$(this).ajaxSubmit();
// 为了防止普通浏览器进行表单提交和产生页面导航(防止页面刷新?)返回false
return false;
});
formSerialize

将表单串行化(或序列化)成一个查询字符串。这个方法将返回以下格式的字符串:name1=value1&name2=value2
可链接(Chainable):不能, 这个方法返回一个字符串。

实例:
var queryString = $(‘#myFormId’).formSerialize();

// 现在可以使用$.get、$.post、$.ajax等来提交数据
$.post(‘myscript.php’, queryString);

fieldSerialize

将表单的字段元素串行化(或序列化)成一个查询字符串。当只有部分表单字段需要进行串行化(或序列化)时,这个就方便了。这个方法将返回以下格式的字符串:name1=value1&name2=value2
可链接(Chainable):不能,这个方法返回一个字符串。

实例:
var queryString = $(‘#myFormId .specialFields’).fieldSerialize();
fieldValue

返回匹配插入数组中的表单元素值。从0.91版起,该方法将总是以数组的形式返回数据。如果元素值被判定可能无效,则数组为空,否则它将包含一个或多于一个的元素值。
可链接(Chainable):不能,该方法返回数组。

实例:
// 取得密码输入值
var value = $(‘#myFormId :password’).fieldValue();
alert(‘The password is: ‘ + value[0]);
resetForm

通过调用表单元素原有的DOM方法,将表单恢复到初始状态。
可链接(Chainable):可以。

实例:
$(‘#myFormId’).resetForm();
clearForm

清除表单元素。该方法将所有的文本(text)输入字段、密码(password)输入字段和文本区域(textarea)字段置空,清除任何select元素中的选定,以及将所有的单选(radio)按钮和多选(checkbox)按钮重置为非选定状态。
可链接(Chainable):可以。

$(‘#myFormId’).clearForm();
clearFields

清除字段元素。只有部分表单元素需要清除时才方便使用。
可链接(Chainable):可以。

$(‘#myFormId .specialFields’).clearFields();
Options对象

ajaxForm和ajaxSubmit都支持众多的选项参数,这些选项参数可以使用一个Options对象来提供。Options只是一个JavaScript对象,它包含了如下一些属性与值的集合:

target

指明页面中由服务器响应进行更新的元素。元素的值可能被指定为一个jQuery选择器字符串,一个jQuery对象,或者一个DOM元素。
默认值:null。

url

指定提交表单数据的URL。
默认值:表单的action属性值

type

指定提交表单数据的方法(method):“GET”或“POST”。
默认值:表单的method属性值(如果没有找到默认为“GET”)。

beforeSubmit

表 单提交前被调用的回调函数。“beforeSubmit”回调函数作为一个钩子(hook),被提供来运行预提交逻辑或者校验表单数据。如果 “beforeSubmit”回调函数返回false,那么表单将不被提交。“beforeSubmit”回调函数带三个调用参数:数组形式的表单数 据,jQuery表单对象,以及传入ajaxForm/ajaxSubmit中的Options对象。表单数组接受以下方式的数据:

[ { name: ‘username’, value: ‘jresig’ }, { name: ‘password’, value: ‘secret’ } ]
默认值:null

success

表单成功提交后调用的回调函数。如果提供“success”回调函数,当从服务器返回响应后它被调用。然后由dataType选项值决定传回responseText还是responseXML的值。
默认值:null

dataType

期望返回的数据类型。null、“xml”、“script”或者“json”其中之一。dataType提供一种方法,它规定了怎样处理服务器的响应。这个被直接地反映到jQuery.httpData方法中去。下面的值被支持:

‘xml’:如果dataType == ‘xml’,将把服务器响应作为XML来对待。同时,如果“success”回调方法被指定, 将传回responseXML值。

‘json’:如果dataType == ‘json’, 服务器响应将被求值,并传递到“success”回调方法,如果它被指定的话。

‘script’:如果dataType == ‘script’, 服务器响应将求值成纯文本。

默认值:null(服务器返回responseText值)

semantic

Boolean flag indicating whether data must be submitted in strict semantic order (slower). Note that the normal form serialization is done in semantic order with the exception of input elements of type=”image”. You should only set the semantic option to true if your server has strict semantic requirements and your form contains an input element of type=”image”.
布 尔标志,表示数据是否必须严格按照语义顺序(slower?)来进行提交。注意:一般来说,表单已经按照语义顺序来进行了串行化(或序列化),除了 type=”image”的input元素。如果你的服务器有严格的语义要求,以及表单中包含有一个type=”image”的input元素,就应该将 semantic设置为true。(译注:这一段由于无法理解,翻译出来可能语不达意,但请达人指正。)
默认值:false

resetForm

布尔标志,表示如果表单提交成功是否进行重置。
Default value: null

clearForm

布尔标志,表示如果表单提交成功是否清除表单数据。
默认值:null

实例:

// 准备好Options对象
var options = {
target:     ‘#divToUpdate’,
url:        ‘comment.php’,
success: function() {
alert(‘Thanks for your comment!’);
} };

// 将options传给ajaxForm
$(‘#myForm’).ajaxForm(options);
注意:Options对象还可以用来将值传递给jQuery的$.ajax方法。如果你熟悉$.ajax所支持的options,你可以利用它们来将Options对象传递给ajaxForm和ajaxSubmit。

英文原版说明:

Form Plugin API

The Form Plugin API provides several methods that allow you to easily manage form data and form submission.

ajaxForm
Prepares a form to be submitted via AJAX by adding all of the necessary event listeners. It does not submit the form. Use ajaxForm in your document’s ready function to prepare your form(s) for AJAX submission. ajaxForm takes zero or one argument. The single argument can be either a callback function or an Options Object.
Chainable: Yes.Note: You can pass any of the standard $.ajax options to ajaxForm

Example:

$('#myFormId').ajaxForm();
ajaxSubmit
Immediately submits the form via AJAX. In the most common use case this is invoked in response to the user clicking a submit button on the form. ajaxSubmit takes zero or one argument. The single argument can be either a callback function or an Options Object.
Chainable: Yes.Note: You can pass any of the standard $.ajax options to ajaxSubmit

Example:

// attach handler to form's submit event 
$('#myFormId').submit(function() { 
    // submit the form 
    $(this).ajaxSubmit(); 
    // return false to prevent normal browser submit and page navigation 
    return false; 
});
formSerialize
Serializes the form into a query string. This method will return a string in the format: name1=value1&name2=value2
Chainable: No, this method returns a String.Example:

var queryString = $('#myFormId').formSerialize(); 
 
// the data could now be submitted using $.get, $.post, $.ajax, etc 
$.post('myscript.php', queryString); 
        
fieldSerialize
Serializes field elements into a query string. This is handy when you need to serialize only part of a form. This method will return a string in the format: name1=value1&name2=value2
Chainable: No, this method returns a String.Example:

var queryString = $('#myFormId .specialFields').fieldSerialize();
fieldValue
Returns the value(s) of the element(s) in the matched set in an array. As of version .91, this method always returns an array. If no valid value can be determined the array will be empty, otherwise it will contain one or more values.
Chainable: No, this method returns an array.Example:

// get the value of the password input 
var value = $('#myFormId :password').fieldValue(); 
alert('The password is: ' + value[0]); 
resetForm
Resets the form to its original state by invoking the form element’s native DOM method.
Chainable: Yes.Example:

$('#myFormId').resetForm();
clearForm
Clears the form elements. This method emptys all of the text inputs, password inputs and textarea elements, clears the selection in any select elements, and unchecks all radio and checkbox inputs.
Chainable: Yes.

$('#myFormId').clearForm();
clearFields
Clears field elements. This is handy when you need to clear only a part of the form.
Chainable: Yes.

$('#myFormId .specialFields').clearFields();

2 thoughts on “jQuery Form Plugin

  1. That’s understandable that cash makes people independent. But what to do if somebody does not have cash? The one way only is to receive the loan and just college loan.

Comments are closed.