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

Jquery插件之ajaxForm ajaxSubmit的理解用法的更多相关文章

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

    我们在平常使用Jquery异步提交表单,一般是在submit()中,使用$.ajax进行.比如: $(function(){ $('#myForm').submit(function(){ $.aja ...

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

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

  3. Jquery插件之ajaxForm简介

    我们平常在使用jQuery异步提交表单的时候,一般都是加载在submit事件中,如下所示: $(document).ready(function(){ $('#myForm').submit(func ...

  4. Jquery插件placeholder的用法

    闲的蛋疼,演示一下Jquery插件placeholder的用法,借助该插件能够轻松实现HTML5中placeholder特效: 效果图: 实现代码: <%@ page language=&quo ...

  5. jQuery插件制作之全局函数用法实例

    原文地址:http://www.jb51.net/article/67056.htm 本文实例讲述了jQuery插件制作之全局函数用法.分享给大家供大家参考.具体分析如下: 1.添加新的全局函数 所谓 ...

  6. JQuery插件之Jquery.datatables.js用法及api

    1.DataTables的默认配置 $(document).ready(function() { $('#example').dataTable(); } ); 示例:http://www.guoxk ...

  7. 理解jquery的$.extend()、$.fn.extend()【jQuery插件机制】

    /** * 操作数据 | DOM 操作 * @description jQuery类添加类方法,可以理解为添加静态方法,将一个或多个对象的内容合并到目标对象 * @use $.fn.zhang() | ...

  8. 第7章 jQuery插件的使用和写法

    第7章 jQuery插件的使用和写法 插件又称扩展,是一种遵循一定规范的应用程序接口写出来的程序. 插件的编写思想基于面向对象. 获取最新的插件可以查看jquery官网:http://plugins. ...

  9. 原创jquery插件treeTable(转)

    由于工作需要,要直观的看到某个业务是由那些子业务引起的异常,所以我需要用树表的方式来展现各个层次的数据. 需求: 1.数据层次分明: 2.数据读取慢.需要动态加载孩子节点: 3.支持默认展开多少层. ...

随机推荐

  1. 如何参与Hibernate-ORM项目

    1.注册 hibernate jira账户,hibernate中的issue和bug都会在此论坛发布 注册地址:https://hibernate.onjira.com 2.创建Hibernate-O ...

  2. 编译filezilla

    编译zilla的时候,需要用到与mysql连接的地方(这里先忽略zila的编译) VC听过mysql connector c++, 下载了1.1.3版本,然后飞安装包,之后从官网上下载boost 把库 ...

  3. Json解析要点

    解析Json时,昨天遇到了新的问题,之前都是解析的数组,不是数组的用类来做. 这是Json串; {"status":"00001","ver" ...

  4. Java的SSH框架

    SSH 为 struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架.   1.业务流程  集成SSH框架的系统从职责上分为四层:表示层.业务逻辑层.数据 ...

  5. 关于window.location.href="delete_emp.do?id"+id;

    ?后面是参数 ?id 就是带参发送这个请求 参数就是id  后面的 +id 貌似 是值

  6. They Are Everywhere

    They Are Everywhere Sergei B., the young coach of Pokemons, has found the big house which consists o ...

  7. 深入理解最强桌面地图控件GMAP.NET ---离线地图

    enjoyeclipse 深入理解最强桌面地图控件GMAP.NET ---离线地图 这章会介绍GMAP.NET的核心功能之一:离线地图.这个功能可以满足很多政府项目.保密项目.或者由于种种原因不能上网 ...

  8. FZU Problem 2221 RunningMan(贪心)

    一开始就跑偏了,耽误了很长时间,我和队友都想到博弈上去了...我严重怀疑自己被前几个博弈题给洗脑了...贪心的做法其实就是我们分两种情况,因为A先出,所以B在第一组可以选择是赢或输,如果要输,那直接不 ...

  9. 转:XPath路径表达式

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) 来选取的. XML 实例文档 我们将在下面的例子中使用这个 XML 文档. & ...

  10. 简单三段式状态机实验1-SOS

    一直想从一段式状态机切换到三段式状态机,从书上和网上不断搜寻三段式案例及方法,感觉很简单,就想拿之前做过的实验把一段式改成三段式,可是写起来并非那么简单,很棘手,改完后也没有成功,尤其状态机里面的计数 ...