Jquery插件之ajaxForm
如今ajax滿(mǎn)天飛,作為重點(diǎn)的form自然也受到照顧。
其實(shí),我們?cè)谄匠J褂肑query異步提交表單,一般是在submit()中,使用$.ajax進(jìn)行。比如:
$(function(){$('#myForm').submit(function(){$.ajax({url:"/WebTest/test/testJson.do",data:$('#myForm').serialize(),dataType:"json",error:function(data){alert(data);},success:function(data){alert(data);}});}); })?這樣的方式掩蓋了form的功能,使它成為了變相的ajax。下面來(lái)看看符合form思想的ajaxForm。
?
ajaxForm:
先下載:http://files.cnblogs.com/china-li/jquery.form.js
兩個(gè)主要的API:ajaxForm() ajaxSubmit()。
ajaxForm()配置完之后,并不是馬上的提交,而是要等submit()事件,它只是一個(gè)準(zhǔn)備。一般用法:
$(document).ready(function() { var options = { target: '#output1', // target element(s) to be updated with server response beforeSubmit: showRequest, // pre-submit callback success: showResponse // post-submit callback // other available options: //url: url // override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute //dataType: null // 'xml', 'script', or 'json' (expected server response type) //clearForm: true // clear all form fields after successful submit //resetForm: true // reset the form after successful submit // $.ajax options can be used here too, for example: //timeout: 3000 }; // bind form using 'ajaxForm' $('#myForm1').ajaxForm(options).submit(function(){return false;}); });這個(gè)是官方的例子,不過(guò)他沒(méi)有最后的提交。提交中返回false,阻止它的默認(rèn)提交動(dòng)作,而是用ajax交互。
其中options的屬性,重要的解釋一下:
target 返回的結(jié)果將放到這個(gè)target下 url 如果定義了,將覆蓋原form的action type get和post兩種方式 dataType 返回的數(shù)據(jù)類(lèi)型,可選:json、xml、script clearForm true,表示成功提交后清除所有表單字段值 resetForm true,表示成功提交后重置所有字段 iframe 如果設(shè)置,表示將使用iframe方式提交表單 beforeSerialize 數(shù)據(jù)序列化前:function($form,options){} beforeSubmit 提交前:function(arr,$from,options){} success 提交成功后:function(data,statusText){} error 錯(cuò)誤:function(data){alert(data.message);}?ajaxSubmit示例:
$(document).ready(function() { var options = { target: '#output2', // target element(s) to be updated with server response beforeSubmit: showRequest, // pre-submit callback success: showResponse // post-submit callback // other available options: //url: url // override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute //dataType: null // 'xml', 'script', or 'json' (expected server response type) //clearForm: true // clear all form fields after successful submit //resetForm: true // reset the form after successful submit // $.ajax options can be used here too, for example: //timeout: 3000 }; // bind to the form's submit event $('#myForm2').submit(function() { // inside event callbacks 'this' is the DOM element so we first // wrap it in a jQuery object and then invoke ajaxSubmit $(this).ajaxSubmit(options); // !!! Important !!! // always return false to prevent standard browser submit and page navigation return false; }); });其中參數(shù)配置大同小異。只是ajaxSubmit()可以任何時(shí)刻都能提交!
?
其他的API:?
$('#myFormId').clearForm(); $('#myFormId .specialFields').clearFields(); $('#myFormId').resetForm(); var value = $('#myFormId :password').fieldValue(); var queryString = $('#myFormId .specialFields').fieldSerialize();?
另外,官方有一個(gè)進(jìn)度條的demo,可以參考一下:http://www.malsup.com/jquery/form/progress.html
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/china-li/archive/2012/12/12/2800144.html
總結(jié)
以上是生活随笔為你收集整理的Jquery插件之ajaxForm的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Apache Nutch 1.6 发布
- 下一篇: 局域网基础知识