如今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. ubuntu server 12.04 源

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 deb http://mirrors.163.com/ubuntu/ precise  ...

  2. opencv 相关一个很好的博客

    http://blog.csdn.net/zouxy09/article/category/1218765 图像卷积与滤波的一些知识点 图像卷积与滤波的一些知识点zouxy09@qq.comhttp: ...

  3. 使用print2flash开发在线文档

    www.print2flash.com 命令行调用: A:\Program Files (x86)\Print2Flash3>p2fServer.exe a.pdf a.swf

  4. discuz 添加板块失败解决办法

    最近把服务器环境升了下级,发现discuz后台添加栏目添加不了了,数据库没变,源代码没变,就突然添加不了了.刚开始添加1个板块成功了,再添加就怎么也添不进去了.只是页面刷新了一下,啥提示没有. 经过一 ...

  5. .NET中公共变量与属性的区别

    在我们的程序中经常会出现以下的代码:  如:     成员变量     public   string   Name;     或者用属性     private   string   name    ...

  6. 关于MySQL中时间格式和取零点的问题

    select * from order where create_time>'2016-05-21 00:00:00'; 不包含2016-05-21 00:00:00时的订单 select * ...

  7. centos搭建nginx环境

    1.yum install  pcre* 2.wget http://nginx.org/download/nginx-1.7.8.tar.gz 3.tar -zxvf nginx-1.7.8.tar ...

  8. nginx php版本隐藏

    配置完一台服务器后,并不是就可以高枕无忧了,前不久刚刚爆发的PHP 5.3.9版本的漏洞也搞得人心惶惶,所以说经常关注安全公告并及时升级服务器也是必要的.一般来说,黑客攻击服务器的首要步骤就是收集信息 ...

  9. 使用virsh命令创建KVM虚拟机快照

    查看虚拟机所在主机和虚拟机名称:[root@node-1 ~]# nova show a88dcf5d-c8b2-46a5-af27-a176d8235c9d|grep hyper| OS-EXT-S ...

  10. Android里merge和include标签的使用

    1.使用<include /> 标签来重用layout代码 如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在andr ...