使用jquery.form.js 进行表单提交,通过回调函数实现页面互动

转自:http://www.cnblogs.com/china-li/archive/2012/12/12/2800144.html

如今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的action
type          get和post两种方式
dataType      返回的数据类型,可选:json、xml、script
clearForm     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();
复制代码

 

另外,官方有一个进度条的demo,可以参考一下:http://www.malsup.com/jquery/form/progress.html


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值