jQuery EasyUI Form Plugin - Form
Override the default defaults with $.fn.form.defaults.
The form provides various methods to perform actions with form fields, such as ajax submission, loading, clearing, etc. When submitting the form, the 'validate' method is called to check if the form is valid.
Usage
Create a simple HTML form. Build the form and assign values to id, action, and method.
<form id="ff" method="post">
<div>
<label for="name">Name:</label>
<input class="easyui-validatebox" type="text" name="name" data-options="required:true" />
</div>
<div>
<label for="email">Email:</label>
<input class="easyui-validatebox" type="text" name="email" data-options="validType:'email'" />
</div>
...
</form>
Make the form an ajax-submitting form.
$('#ff').form({
url:...,
onSubmit: function(){
// do some check
// return false to prevent submit;
},
success:function(data){
alert(data)
}
});
// submit the form
$('#ff').submit();
To 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';
}
});
Handling Submit Response
Submitting an ajax form is straightforward. Users can obtain response data when the submission is complete. Note that the response data is raw data from the server. Parsing the response data requires obtaining the correct data.
For example, assuming the response data is in JSON format, a typical response data looks like this:
{
"success": true,
"message": "Message sent successfully."
}
Now handle 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)
}
}
});
Properties
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 submission is successful. |
onBeforeLoad | param | Triggered before making a request to load data. Return false to cancel the action. |
onLoadSuccess | data | Triggered when the form data is loaded. |
onLoadError | none | Triggered when some error occurs while loading form data. |
Methods
Name | Parameters | Description |
---|---|---|
submit | options | Perform the submit action, options parameter is an object that includes the following properties: <br>url: The action URL <br>onSubmit: Callback function before submission <br>success: Callback function after successful submission <br> <br>Example: $.messager.progress(); // display the progress bar<br>$('#ff').form('submit', {<br>url: ...,<br>onSubmit: function(){<br>var isValid = $(this).form('validate');<br>if (!isValid){<br>$.messager.progress('close'); // hide progress bar if the form is invalid<br>}<br>return isValid; // return false will stop the form submission<br>},<br>success: function(){<br>$.messager.progress('close'); // hide progress bar after successful submission<br>}<br>}); |
load | data | Load records to populate the form. The data parameter can be a string or an object type, string as a remote URL, otherwise as a local record. <br> <br>Example: $('#ff').form('load','get_data.php'); // load from URL<br>$('#ff').form('load',{<br>name:'name2',<br>email:'[email protected]',<br>subject:'subject2',<br>message:'message2',<br>language:5<br>}); |
clear | none | Clear the form data. |
reset | none | Reset the form data. This method is available since version 1.3.2. |
validate | none | Perform form field validation, returns true if all fields are valid. This method works with the validatebox plugin. |
enableValidation | none | Enable validation. This method is available since version 1.3.4. |
disableValidation | none | Disable validation. This method is available since version 1.3.4. |