JQuery.form.js is a form plugin that supports AJAX form submissions and Ajax file uploads.
<!DOCTYPE HTML><HTML><Head> <Metaname= "Viewport"content= "Width=device-width" /> <title>Index</title> <Scriptsrc= "~/scripts/jquery-1.6.2.js"></Script> <Scriptsrc= "~/scripts/jquery.form.js"></Script></Head><Body> <Div> <formID= "Ajaxform"Method= "POST"Action= "/home/index"enctype= "Multipart/form-data"> <inputtype= "text"name= "Name" /> <inputtype= "text"name= "Sex" /> <inputtype= "File"name= "File" /> <Buttontype= "Submit"ID= "Btnsubmit">Submit 1</Button> </form> <ButtonID= "Btnbutton"type= "button">Submit 2</Button> </Div> <Scripttype= "Text/javascript"> $(function () { $("#ajaxForm"). Ajaxform (function() {alert ("Submit Success 1"); }); $("#ajaxForm"). Submit (function () { $( This). Ajaxsubmit (function() {alert ("Submit Success 1"); }); return false; }); $("#btnButton"). Click (function () { $("#ajaxForm"). Ajaxsubmit (function() {alert ("Submit Success 2"); }); return false; }); }); </Script></Body></HTML>
Api
Ajaxform |
Add all the required event listeners to prepare for the AJAX submission form. Ajaxform cannot submit the form. In the ready function of document, use Ajaxform to prepare the form for the AJAX submission. |
accepts 0 or 1 parameters. A parameter can be a callback function, or it can be an options object. |
$ ("#formid"). Ajaxform (); |
Ajaxsubmit |
Submit a form using AJAX. |
accepts 0 or 1 parameters. A parameter can be a callback function, or it can be an options object. |
$ ("#formid"). Ajaxsubmit (); Or $ ("#formid"). Submit (function () { $ (this). Ajaxsubmit (); return false; }); |
Formserialize |
Serializes (or serialize) a form into a query string. This method returns a string in the following format: Name1=value1&name2=value2. |
No |
$ ("#formid"). Formserialize (); |
Fieldserialize |
Serializes (or serialize) the field elements of a form into a single query string. This is handy when only a subset of the form fields need to be serialized (or serializable). Returns a string in the following format: Name=value1&name2=value2. |
No |
$ ("#formid. Specialfields"). Fieldserialize (); |
Fieldvalue |
Returns the value of the form element that matches the inserted array. The method returns data in the form of an array. If the element value is determined to be invalid, the array is empty. |
No |
$ ("#formid:p assword"). Fieldvalue (); |
Resetform |
Restores the form to its original state. |
No |
$ ("#formid"). Resetform (); |
ClearForm |
Clears the form element. This method resets all text, password, textarea, clears the selection in the Select element, and resets all radio and CheckBox buttons to the unselected state. |
No |
$ ("#formid"). ClearForm (); |
Clearfields |
Clears the field element. Only some form elements are easy to use when they need to be cleared. |
No |
$ ("#formid. Specialfields"). Clearfields (); |
Options Object
Both Ajaxform and Ajaxsubmit support a wide range of option parameters, which can be provided using an options object.
Target |
Indicates the element in the page that is updated by the server response. The value of the element may be specified as a jquery selector string, a jquery object, and a DOM element. |
Default value: null |
Url |
Specifies the URL to submit the form data. |
Default value: The Action property value of the form |
Type |
Specifies the method by which the form data is submitted: "GET" or "POST". |
Default value: GET |
Beforesubmit |
The callback function that was called before the form was submitted. If the callback function returns false, the form will not be committed. The callback function takes three invocation parameters: the form data as an array, the JQuery form object, and the options object in the incoming ajaxform/ajaxsubmit. |
Default value: null |
Success |
callback function that is called after the form has been successfully committed. The datatype option value then determines whether to return ResponseText or responsexml values. |
Default value: null |
DataType |
Data type returned: null, XML, script, json one of them. |
Default value: null |
Resetform |
Indicates whether the form submission succeeds if it is reset. |
Default value: null |
ClearForm |
Indicates whether the form data is purged if the form submission succeeds. |
varoptions={target:' #output ',//put the contents returned by the server into an element with ID outputBeforesubmit:showrequest,//Pre-commit callback functionSuccess:showresponse,//Post-Commit callback function //Url:url,//default is the action of form, if stated, Overrides //Type:type,//The default value is the method of form ("GET" or "POST"), and if declared, overrides //datatype:null,//html (default), XML, script, JSON accept server-side returned types //Clearform:true,//After successful commit, clears the values of all form elements //Resetform:true,//After successful commit, resets the values of all form elementstimeout:3000//limit the time of the request, when the request is greater than 3 seconds, jump out of the request}functionshowrequest (FormData, Jqform, options) {//formData: An Array object, when the form is submitted, the form plug-in automatically submits the data in an AJAX format such as [{name:user,value:val},{name:pwd,value:pwd}] //Jqform:jquery object that encapsulates the elements of the form //Options:options Object varQuerystring=$.param (FormData);//name=1&address=2 varFORMELEMENT=JQFORM[0];//convert Jqform to DOM object varAddress=formelement.address.value;//accessing the DOM elements of Jqform return true;//The form will be submitted as long as it does not return false, where the form elements can be validated}functionShowresponse (responsetext,statustext) {//Datatype=xml varname=$ ("name", Responsexml). text (); varaddress=$ ("Address", Responsexml). text (); $("#xmlout"). HTML (name+ "" +address); //Datatype=json$ ("#jsonout"). HTML (data.name+ "" +data.address);}
JQuery.form.js use