一般情况下其实提交表单我们将数据$("form").serialize()序列化之后用$.ajax$.post就可以实现,但是jQuery自带的方法仅支持异步提交数据,但不支持文件上传,除非我们自己用XMLHttpRequest写一个方法。
好在jquery.form.js自己实现了一套ajax的方法,支持异步文件上传并获取响应结果,这才是我使用jQuery.form的主要原因。
当然,小文件你也可以将其base64序列化,以字符串方式回传服务器,而不使用这个插件


介绍

jQuery Form插件能够让我们非常方便的实现form表单的AJAX异步提交,并且可以完全自定义提交的数据!

官网:http://plugins.jquery.com/form/
github:https://github.com/malsup/form
文档:http://jquery.malsup.com/form/


使用

1、引用JS

  1. <script src="./jquery.js"></script>
  2. <script src="./jquery.form.js"></script>

2、声明表单

  1. <formid="myForm"action="comment.php"method="post">
  2. Name: <inputtype="text"name="name"/>
  3. Comment: <textareaname="comment"></textarea>
  4. <inputtype="submit"value="Submit Comment"/>
  5. </form>

3、ajax提交

  1. $(document).ready(function(){
  2. // bind 'myForm' and provide a simple callback function
  3. $('#myForm').ajaxForm(function(){
  4. alert("Thank you for your comment!");
  5. });
  6. });

API说明

ajaxForm

并不会提交Form,在document的ready函数中通过给指定form添加事件监听使其成为AJAX异步表单。

  1. $('#myFormId').ajaxForm();

ajaxSubmit

立即通过AJAX提交表单,一般是用来替换绑定表单的提交事件

  1. //绑定submit事件
  2. $('#myFormId').submit(function(){
  3. //异步提交
  4. $(this).ajaxSubmit();
  5. // return false 用来阻止浏览器的提交与跳转
  6. returnfalse;
  7. });

fieldSerialize

和$.serialize大同小异,序列化表单数据

  1. var queryString = $('#myFormId .specialFields').fieldSerialize();

resetForm

将表单恢复到初始状态。

  1. $('#myFormId').resetForm();

clearForm

清除表单元素。该方法将所有的text、password、textarea置空,清除select元素中的选定,以及所有radio按钮和checkbox按钮重置为非选定状态。

  1. $('#myFormId').resetForm();

clearFields

清除字段元素。只有部分表单元素需要清除时方便使用。

  1. $('#myFormId .specialFields').clearFields();

参数示例

var options = {
data: { key1: 'value1', key2: 'value2' }, //连同表单一起提交的额外数据
target: '#output1', // 把服务器返回的内容放入id为output的元素中
beforeSerialize: function($form, options) {
// return false to cancel submit
},
beforeSubmit: showRequest, // pre-submit callback
success: showResponse, // post-submit callback
error: function(){},//在错误中调用的回调函数。 //other available options:
url: url //默认是form的action,如果申明,则会覆盖
type: type // 默认值是form的method("GET" or "POST"),如果声明,则会覆盖
dataType: null // null(默认)、xml、script、json接受服务器端返回的类型
clearForm: true // 成功提交后,清除所有表单元素的值
resetForm: true // 成功提交后,重置所有表单元素的值 //超时时间
timeout: 3000
}; // pre-submit callback
function showRequest(formData, jqForm, options) {
// The array of form data takes the following form:
// [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
// formData is an array; here we use $.param to convert it to a string to display it
// but the form plugin does this for you automatically when it submits the data
var queryString = $.param(formData); // jqForm is a jQuery object encapsulating the form element. To access the
// DOM element for the form do this:
// var formElement = jqForm[0]; alert('About to submit: \n\n' + queryString); // here we could return false to prevent the form from being submitted;
// returning anything other than false will allow the form submit to continue
return true;
} // post-submit callback
function showResponse(responseText, statusText, xhr, $form) {
// for normal html responses, the first argument to the success callback
// is the XMLHttpRequest object's responseText property // if the ajaxForm method was passed an Options Object with the dataType
// property set to 'xml' then the first argument to the success callback
// is the XMLHttpRequest object's responseXML property // if the ajaxForm method was passed an Options Object with the dataType
// property set to 'json' then the first argument to the success callback
// is the json data object returned by the server alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');
}

jQuery.form开发手记的更多相关文章

  1. jquery ajax 开发手记

    1.json解析的格式要求更严格了,必须全部加引号,否则无法识别 {"result":"false"} 2.ashx如果要使用Session需要继承接口IReq ...

  2. jQuery.Flot开发手记

    目录 介绍 使用 自定义参数 自定义图例 自定义坐标 自定义数据序列 自定义网格 其他 鼠标停留在图表节点时显示tooltip 介绍 项目地址:http://www.flotcharts.org/ A ...

  3. jQuery MiniUI 开发指南+API组件参考手册

    jQuery MiniUI 开发指南 本文档将逐步的讲解jQuery MiniUI的方方面面,从此您将踏上jQuery MiniUI的深入探索之旅.                 1.Hello M ...

  4. 使用Ajax以及Jquery.form异步上传图片

    一.前言 之前做图片上传一直用的第三方插件,Uploadify  这个应该是用的比較多的,相同也用过别的,在方便了自己的同一时候也非常赞叹人家的功能. 思来想去,仅仅会用别的人东西,始终自己学到的少, ...

  5. 使用jQuery.form库中ajaxSubmit提交表单时遇到的一些问题

    初入前端,网上找的很多资料都不够详细,导致遇到很多问题,现记录如下: 1.首先引入 <script src="~/Scripts/jquery-1.10.2.js">& ...

  6. 关于jQuery Form Plugin使用心得

    吐槽一下先 好久没开发了,今天遇到一个客户form提交的问题,想把form提交从同步变成ajax的异步方式,在网页接受返回来的数据,使用的是jquery from插件,于是网上搜了一圈,博客园,csd ...

  7. 怎么利用jquery.form 提交form

    说明:开发环境 vs2012 asp.net mvc c# 利用jQuery.form.js提交form 1.HTML前端代码 <%@ Page Language="C#" ...

  8. [转]Nodejs开发框架Express4.x开发手记

    Express: ?web application framework for?Node.js? Express 是一个简洁.灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮 ...

  9. jQuery为开发插件提拱了两个方法:jQuery.fn.extend(); jQuery.extend();

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); jQuery.fn jQuery.fn = jQuery.prototype ...

随机推荐

  1. c# 实现点击下载功能

    转自百度知道 private void DownLoad(string strName, string strPath) { string fileName = strName;//客户端保存的文件名 ...

  2. DataType--时间类型

    SQL SERVER 存储时间的方式和存放浮点数的方式类似,存放时按照一定公式算出一个数值,存放到页面,在读取时按照公式求算出时间值,再按照默认或指定的时间格式展示给用户. 如果存放DATETIME数 ...

  3. EasyUI控件combobox重复请求后台,dialog窗口数据异常

    最近在用Easy UI+Dapper+MVC4 开发一个财务收款系统,其中就发现一些小问题,供有需要的人参考. 1.EasyUI控件combobox 数据绑定 出现重复请求后台 上代码: <td ...

  4. GitHub团队协作流程

    说来惭愧,这么长时间,第一次参与修改开源项目,所以整理了一份GitHub团队协作流程,作为备忘,文章大部分内容参考https://www.cnblogs.com/schaepher/p/4933873 ...

  5. 分享我访问google的方法

    对于程序员来说,有很多技术问题还是通过互联网搜索来解决的.所以百度.google对于我们是多少的重要.但是GOOGLE现在无法访问了.怎么办呢.以下是我访问google的方法. 首先自己制作了一个简单 ...

  6. react.js学习之路四

    针对学习react.js中,我感觉最大的疑惑点就是bind(this)的绑定和指向问题了,我被这个问题弄的头昏,有时候调用组件的时候,直接显示undefined,不存在这个组件,当时的心情是崩溃的,整 ...

  7. HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories

    Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262 ...

  8. Flink学习笔记:Connectors之kafka

    本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...

  9. 使用webpack开发ES6程序的正确姿势

    1.cnpm install babel-loader babel-core babel-preset-es2015 -D 2.cnpm install babel-plugin-transform- ...

  10. iOS苹果和微信中音频和视频实现自动播放的方法

    通过下面的方式可以解决,在iPhone手机微信中正常自动播放. 必须在微信Weixin JSAPI的WeixinJSBridgeReady才能生效,猜测微信接口做了处理~ <audio prel ...