query外掛程式之ajaxForm ajaxSubmit的理解用法

來源:互聯網
上載者:User

標籤:class   blog   code   http   tar   ext   

如今ajax滿天飛,作為重點的form自然也受到照顧。

其實,我們在平常使用Jquery非同步提交表單,一般是在submit()中,使用$.ajax進行。比如:

 
    $(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。下面來看看符合form思想的ajaxForm。

 

ajaxForm:

先下載:http://files.cnblogs.com/china-li/jquery.form.js

兩個主要的API:ajaxForm() ajaxSubmit()。

ajaxForm()配置完之後,並不是馬上的提交,而是要等submit()事件,它只是一個準備。一般用法:

 
$(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;}); });
 

這個是官方的例子,不過他沒有最後的提交。提交中返回false,阻止它的預設提交動作,而是用ajax互動。

其中options的屬性,重要的解釋一下:

 
target        返回的結果將放到這個target下url           如果定義了,將覆蓋原form的actiontype          get和post兩種方式dataType      返回的資料類型,可選:json、xml、scriptclearForm     true,表示成功提交後清除所有表單欄位值resetForm     true,表示成功提交後重設所有欄位iframe        如果設定,表示將使用iframe方式提交表單beforeSerialize    資料序列化前:function($form,options){}beforeSubmit  提交前:function(arr,$from,options){}success       提交成功後:function(data,statusText){}error         錯誤: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;     }); }); 
 

其中參數配置大同小異。只是ajaxSubmit()可以任何時刻都能提交!

 

其他的API: 

 
$(‘#myFormId‘).clearForm();$(‘#myFormId .specialFields‘).clearFields();$(‘#myFormId‘).resetForm();var value = $(‘#myFormId :password‘).fieldValue();var queryString = $(‘#myFormId .specialFields‘).fieldSerialize();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.