Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery EasyUI Form Plugin - Form

jQuery EasyUI Form Plugin - Form


jQuery EasyUI Plugins jQuery EasyUI Plugins


Override the default defaults via $.fn.form.defaults.

The form provides several methods to perform actions on form fields, such as AJAX submit, load, clear, and so on. When submitting a form, the 'validate' method is called to check whether the form is valid.

Usage

Create a simple HTML form. Build the form and assign values to id, action, and method.



                 
                 
...

Make the form an AJAX-submitted form



$('#ff').form({

    url:...,

    onSubmit: function(){

        // do some check

        // return false to prevent submit;

    },

    success:function(data){

        alert(data)

    }

});

// submit the form

$('#ff').submit();

Perform a submit action



// call 'submit' method of form plugin to submit the form

$('#ff').form('submit', {

    url:...,

    onSubmit: function(){

        // do some check

        // return false to prevent submit;

    },

    success:function(data){

        alert(data)

    }

});

Submit with additional parameters



$('#ff').form('submit', {

    url:...,

    onSubmit: function(param){

        param.p1 = 'value1';

        param.p2 = 'value2';

    }

});

Handle the submit response

Submitting an AJAX form is very simple. When the submission is complete, the user can obtain the response data. Note that the response data is the raw data from the server. Parsing the response data is required to obtain the correct data.

For example, assuming the response data is in JSON format, a typical response looks like this:



{

    "success": true,

    "message": "Message sent successfully."

}

Now process the JSON string in the 'success' callback function.



$('#ff').form('submit', {

    success: function(data){

        var data = eval('(' + data + ')'); // change the JSON string to javascript object

        if (data.success){

            alert(data.message)

        }

    }

});

property

Name Type Description Default value
url string The form action URL to submit to. null

Events

Name Parameters Description
onSubmit param Triggered before submission; return false to prevent the submit action.
success data Triggered when the form is submitted successfully.
onBeforeLoad param Triggered before the request to load data is sent. Return false to cancel this action.
onLoadSuccess data Triggered when form data is loaded.
onLoadError none Triggered when an error occurs while loading form data.

Method

Name Parameters Description
submit options Performs the submit action; the options parameter is an object containing the following properties:
 url:动作的 URL       

 onSubmit:提交之前的回调函数       

 success:提交成功之后的回调函数       

   

 下面的实例演示如何提交一个有效表单,避免重复提交表单。     

$.messager.progress(); // display the progress bar $('#ff').form('submit', { url: ..., onSubmit: function(){ var isValid = $(this).form('validate'); if (!isValid){ $.messager.progress('close'); // hide progress bar while the form is invalid } return isValid; // return false will stop the form submission }, success: function(){ $.messager.progress('close'); // hide progress bar while submit successfully } });| onSelect | event | load | data | Load records to fill the form. The data parameter can be a string or an object; a string is treated as a remote URL, otherwise as a local record.

 代码实例:     

$('#ff').form('load','get_data.php');    // load from URL

$('#ff').form('load',{ name:'name2', email:'mymail@gmail.com', subject:'subject2', message:'message2', language:5 });| onSelect | event | clear | none | Clear the form data.| onSelect | event | reset | none | Reset the form data. This method is available since version 1.3.2.| onSelect | event | validate | none | Perform form field validation, returning true when all fields are valid. This method is used with the validatebox plugin.| onSelect | event | enableValidation | none | Enable validation. This method is available since version 1.3.4.| onSelect | event | disableValidation | none | Disable validation. This method is available since version 1.3.4. |


jQuery EasyUI Plugins jQuery EasyUI Plugins