如今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();

转载于:https://www.cnblogs.com/guolanzhu/p/3820344.html

Jquery插件之ajaxForm ajaxSubmit的理解用法相关推荐

  1. query插件之ajaxForm ajaxSubmit的理解用法

    如今ajax满天飞,作为重点的form自然也受到照顾. 其实,我们在平常使用Jquery异步提交表单,一般是在submit()中,使用$.ajax进行.比如: $(function(){$('#myF ...

  2. Jquery插件之ajaxForm

    如今ajax满天飞,作为重点的form自然也受到照顾. 其实,我们在平常使用Jquery异步提交表单,一般是在submit()中,使用$.ajax进行.比如: $(function(){$('#myF ...

  3. jquery插件jcrop的初步认识与用法

    最近在写项目的过程中,需要用到图像裁切上传,所以接触了插件jcrop.这个插件的裁切功能主要是获取裁切图片后的x坐标,y坐标与裁切图片的宽度和高度.该插件运行后,dom格式如下 关于该插件的源码,以及 ...

  4. php jquery validate remote,jquery插件validate里面的remote参数用法

    validate验证在进行异步数据库查询验证的过程中用到了remote这个参数 remote里面有url,dataType,data,type等等这几个参数,当data不写的时候默认是当前被验证的字段 ...

  5. jQuery form插件之ajaxForm()和ajaxSubmit()

    代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> < ...

  6. jQuery form插件之ajaxForm()和ajaxSubmit()的可选参数项对象

    原文:http://www.jb51.net/article/78536.htm Form Plugin API 里提供了很多有用的方法可以让你轻松的处理表单里的数据和表单的提交过程. 测试环境:部署 ...

  7. JQeury form插件的ajaxForm方法和ajaxSubmit方法的区别

    为什么80%的码农都做不了架构师?>>>    /*** ajaxForm() provides a mechanism for fully automating form subm ...

  8. jquery.form.js的ajaxSubmit和ajaxForm使用(用于多文件上传以及其他参数的混合提交)

    jquery.form.js的ajaxSubmit和ajaxForm使用(用于多文件上传以及其他参数的混合提交) ajaxSubmit 和ajaxForm区别 ajaxForm ajaxForm()不 ...

  9. html使用定时器timer,jquery插件jTimer jquery定时器的用法举例

    本节内容: jquery插件jTimer,jQuery定时器. 需求: 按时间间隔执行一个任务,当满足一定条件时停止执行. 1,插件用法: 复制代码 代码示例: (function ($) { $.e ...

最新文章

  1. 一文带你了解 MySQL 中的各种锁机制!
  2. ProjectEuler 4
  3. springcloud学习计划
  4. html5简单游戏案例,HTML5存储(带一个粗糙的打怪小游戏案例)
  5. 关于WebView 控件,你了解多少?
  6. 中缀表达式计算、后缀表达式计算、中缀转后缀
  7. Java能做什么?学完Java可以从事什么工作呢?
  8. Android4.2.2的Stagefright维护编解码器的数据流
  9. Git笔记-Connection reset by 13.229.188.59 port 22 fatal: Could not read from remote repository.
  10. 求二进制数中1的个数(转)
  11. deployment:声明式的升级应用
  12. 苹果因芯片短缺优先生产iPhone 13 常规iPad平均交付时间超7周
  13. 第四范式完成超10亿元C轮融资,估值12亿美元
  14. pythonexcel怎么合并_Python把多个Excel合并成一个Excel
  15. java图书馆抢座系统_java毕业设计_springboot框架的图书馆座位预约占座
  16. 支付宝AR实景红包寻宝攻略
  17. 简单双拼入门,一看就会,看完就用.
  18. typora笔记使用base64编码图片
  19. dd腾讯云阿里云并安装相应代理加速github
  20. FCPX插件:视频转场插件Stupid Raisins Panel Pop

热门文章

  1. spring cloud config client refresh过程
  2. 用批处理读取特定注册表项的路径值
  3. 《谈谈认同》这个文章有点晦涩 写的不太好
  4. 公司内部z区域网络升级可行性方案
  5. Mysql Order By 字符串排序,mysql 字符串order by
  6. SpringBoot配置RestTemplate的代理和超时时间
  7. (原)caffe在ubuntu中设置GPU的ID号及使用多个GPU
  8. 原生js 实现购物车价格和总价 统计
  9. 《Spring Security3》第四章第一部分翻译下(自定义的UserDetailsServic
  10. CSS布局与表格布局之我见 [2007年8月3日]