jQuery ajaxSubmit 实现ajax提交表单局部刷新


Posted in Javascript onJuly 04, 2016

AJAX简介

AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。

AJAX 不是新的编程语言,而是一种使用现有标准的新方法。

AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。

需要引入 : jquery-form.js

使用说明:

Java代码

$(document).ready(function() { 
var options = { 
target: '#mydiv', // 需要刷新的区域 
//beforeSubmit: showRequest, // 提交前调用的方法 
//success: showResponse // 返回后笤俑的方法 
// other available options: 
//url: url // 提交的URL, 默认使用FORM ACTION 
//type: type // 'get' or 'post', override for form's 'method' attribute 
//dataType: null // 'xml', 'script', or 'json' (expected server response type) 
//clearForm: true // 是否清空form 
//resetForm: true // 是否重置form 
// $.ajax options can be used here too, for example: 
//timeout: 3000 
}; 
// 绑定FORM提交事件 
$('#myForm').submit(function() { 
$(this).ajaxSubmit(options); 
// !!! Important !!! 
// always return false to prevent standard browser submit and page navigation 
return false; 
}); 
});

调用前后方法:

Java代码

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
// 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) { 
// for normal html responses, the first argument to the success callback 
// is the XMLHttpRequest object's responseText property 
// if the ajaxSubmit 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 ajaxSubmit 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.'); 
}

项目中可以写一个公用的方法:

Java代码

// 局部提交表单 
function formSubmit(formId, divId, url) { 
$('#' + formId).submit(function() { 
$(this).ajaxSubmit( { 
target : '#' + divId, 
url : url, 
error : function() { 
alert('加载页面' + url + '时出错!') 
} 
}); 
return false; 
}); 
}

=====================================================================

事例 刷新TABLE:

1.把TABLE单独放一个JSP,主页面 include TABLE页面。

2.主页面:

Java代码

window.onload=function (){ 
//AJAX 提交FORM刷新TABLE 
$('#queryForm').submit(function() { 
$(this).ajaxSubmit( { 
target : '#table1' 
}); 
return false; 
}); 
}

点击查询按钮 提交FORM。

3.JAVA:FORM提交调用的方法和 普通的ACTION写法一样, STRUTS里配置该ACTION跳转到 那个单独的TABLE JSP页面,返回成功后,就会看到刷新了TABLE。

Java代码

/** 
* AJAX汇总查询 未公开知情人列表 
* 部门合规风控专员 汇总查询 
*/ 
public String ajaxgatherinsiderlist() { 
//相关业务数据查询 
return SUCCESS; 
}

以上所述是小编给大家介绍的jQuery ajaxSubmit 实现ajax提交表单局部刷新 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
JavaScript 实现模态对话框 源代码大全
May 02 Javascript
简单实例处理url特殊符号&处理(2种方法)
Apr 02 Javascript
js类式继承的具体实现方法
Dec 31 Javascript
javascript删除数组元素并且数组长度减小的简单实例
Feb 14 Javascript
JavaScript基于setTimeout实现计数的方法
May 08 Javascript
Bootstrap布局方式详解
May 27 Javascript
javascript滚轮控制模拟滚动条
Oct 19 Javascript
AngularJS深入探讨scope,继承结构,事件系统和生命周期
Nov 02 Javascript
jQuery实现字符串全部替换的方法
Dec 12 Javascript
JS区分Object与Aarry的六种方法总结
Feb 27 Javascript
Vue实现内部组件轮播切换效果的示例代码
Apr 07 Javascript
15 分钟掌握vue-next响应式原理
Oct 13 Javascript
jQuery通用的全局遍历方法$.each()用法实例
Jul 04 #Javascript
JS构造函数与原型prototype的区别介绍
Jul 04 #Javascript
js中使用使用原型(prototype)定义方法的好处详解
Jul 04 #Javascript
js与jquery正则验证电子邮箱、手机号、邮政编码的方法
Jul 04 #Javascript
浅谈js构造函数的方法与原型prototype
Jul 04 #Javascript
全面了解js中的script标签
Jul 04 #Javascript
jQuery基础_入门必看知识点
Jul 04 #Javascript
You might like
解析crontab php自动运行的方法
2013/06/24 PHP
使用 laravel sms 构建短信验证码发送校验功能
2017/11/06 PHP
Laravel实现ApiToken认证请求
2019/10/14 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
学习jquery之一
2007/04/27 Javascript
关于JavaScript的一些看法
2009/05/27 Javascript
基于jQuery实现下拉收缩(展开与折叠)特效
2012/12/25 Javascript
文本框(input)获取焦点(onfocus)时样式改变的示例代码
2014/01/10 Javascript
textarea不能通过maxlength属性来限制字数的解决方法
2014/09/01 Javascript
JavaScript中的标签语句用法分析
2015/02/10 Javascript
js实现下拉列表选中某个值的方法(3种方法)
2015/12/17 Javascript
深入解析JavaScript中的立即执行函数
2016/05/21 Javascript
jQuery EasyUI datagrid在翻页以后仍能记录被选中行的实现代码
2016/08/15 Javascript
javascript 显示全局变量与隐式全局变量的区别
2017/02/09 Javascript
vue-router相关基础知识及工作原理
2018/03/16 Javascript
javascript数据结构之多叉树经典操作示例【创建、添加、遍历、移除等】
2018/08/01 Javascript
原生js实现针对Dom节点的CRUD操作示例
2019/08/26 Javascript
JS获取表格视图所选行号的ids过程解析
2020/02/21 Javascript
[03:42]2014DOTA2国际邀请赛 第三日比赛排位扑朔迷离
2014/07/12 DOTA
Python中的条件判断语句与循环语句用法小结
2016/03/21 Python
Python 多线程的实例详解
2017/09/07 Python
Python 将pdf转成图片的方法
2018/04/23 Python
python excel使用xlutils类库实现追加写功能的方法
2018/05/02 Python
Python实现将Excel转换成xml的方法示例
2018/08/25 Python
Python实现图片转字符画的代码实例
2019/02/22 Python
Python read函数按字节(字符)读取文件的实现
2019/07/03 Python
python爬虫selenium和phantomJs使用方法解析
2019/08/08 Python
Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统
2019/09/05 Python
HTML5 video 事件应用示例
2014/09/11 HTML / CSS
一道Delphi上机题
2012/06/04 面试题
商场拾金不昧表扬信
2014/01/13 职场文书
会展策划与管理专业求职信
2014/06/09 职场文书
独生子女证明范本
2015/06/19 职场文书
2016年优秀少先队辅导员事迹材料
2016/02/26 职场文书
php中pcntl_fork详解
2021/04/01 PHP
CSS3 制作精美的定价表
2021/04/06 HTML / CSS