如今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/Alight/p/3807696.html

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

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

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

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

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

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

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

  4. Jquery插件之ajaxForm

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

  5. jQuery Live Query 插件

    http://www.cnblogs.com/sunjing/archive/2008/12/06/1349097.html 上篇日志里写到的那个问题其实有种更简单的解决办法,就是使用jquery的L ...

  6. TypeScript里的工具类型Partial的理解用法

    TypeScript里的工具类型Partial的理解用法 Partial 可以快速把某个接口类型中定义的属性类型变成可选的(Optional): 其实Partial源码很简单 Partial 可以快速 ...

  7. 论文、报告及教案公式编辑:图片公式转换Mathpix snipping Tool、快速编辑神器AxMath插件操作使用的几种用法(最详细精致)

    图片公式转换Mathpix snipping Tool及快速编辑神器AxMath插件操作使用的几种用法(最详细精致) [文章内容较多, 点击目录链接可直达标题内容] 文章目录 图片公式转换Mathpi ...

  8. jquery-form插件的ajaxForm和ajaxSubmit的用法与区别

    原文地址:https://www.cnblogs.com/fire-dragon/p/6708718.html 在使用这两种方法之前,首先需要安装form.js的插件,下载就不放了,网上一搜就有: & ...

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

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

最新文章

  1. 燃烧青春和诗意的IT人生
  2. python两个数相加时_怎么用python让两个小数相加
  3. C++new和delete实现原理(汇编解释)
  4. ssh(Spring+Spring mvc+hibernate)——DeptServiceImpl.java
  5. 《Python Cookbook 3rd》笔记(1.9):查找两字典的相同点
  6. fastjson反序列化过滤字段属性_原创干货 | 从RMI入门到fastjson反序列化RCE
  7. Docker学习总结(24)——在Docker中监视Java应用程序的5种方法
  8. mysql密码加强_MySQL密码增强插件
  9. DES加密/解密类。
  10. 文献管理三剑客之Mendeley最新版使用小记2
  11. linux时间戳简介,linux 时间戳
  12. PCL安装与配置(Windows10+VS2017+pcl1.8.1 )
  13. 期货市场的投资者适当性原则
  14. bfv同态加密_全同态加密BFV-(section 2-SHE)
  15. 去除水印PNAS-latex
  16. 字符串在html中的页面中的换行
  17. Python3.6安装PyQt5的方法
  18. Spring的四种注入方式
  19. [附源码]Nodejs计算机毕业设计基于HTML5的智能仓储管理系统Express(程序+LW)
  20. bootstratreeview树形下拉框

热门文章

  1. C#温故而知新学习系列之.NET框架高级特性—概述.NET框架中的反射(一)
  2. dBm与Vpp相互转换公式
  3. logo语言linux,用 screenfetch 和 linux_logo 显示带有酷炫 Linux 标志的基本硬件信息
  4. 解决Moodle日历乱码的最佳方案
  5. 如何修改word中的单位信息、用户信息及文档属性
  6. “僵尸病毒”入侵全球电脑,7.5万部电脑中招(来源:广州日报)
  7. python读写excel数据--pandas
  8. WebSocket实战之————Workerman服务器的安装启动
  9. 专访Matt Klein关于在Lyft构建Envoy的问答
  10. Android--百度地图密钥申请+环境配置(一)