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 相关文章推荐
jQuery之自动完成组件的深入解析
Jun 19 Javascript
Function.prototype.bind用法示例
Sep 16 Javascript
JS实现固定在右下角可展开收缩DIV层的方法
Feb 13 Javascript
React学习笔记之条件渲染(一)
Jul 02 Javascript
网页中的图片查看器viewjs使用方法
Jul 11 Javascript
关闭Vue计算属性自带的缓存功能方法
Mar 02 Javascript
Vue父子组件之间的通信实例详解
Sep 28 Javascript
js实现搜索栏效果
Nov 16 Javascript
react脚手架如何配置less和ant按需加载的方法步骤
Nov 28 Javascript
在微信小程序中保存网络图片
Feb 12 Javascript
three.js利用卷积法如何实现物体描边效果
Nov 27 Javascript
vue Treeselect 树形下拉框:获取选中节点的ids和lables操作
Aug 15 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
DOTA2 1月28日更新:监管系统降临刀塔世界
2021/01/28 DOTA
PHP+javascript模拟Matrix画面
2006/10/09 PHP
Zend Framework 2.0事件管理器(The EventManager)入门教程
2014/08/11 PHP
PHP实现二维数组按某列进行排序的方法
2016/11/18 PHP
用Javascript实现Sleep暂停功能代码
2010/09/03 Javascript
jQuery基本过滤选择器使用介绍
2013/04/18 Javascript
JQuery创建DOM节点的方法
2015/06/11 Javascript
JS中使用apply方法通过不同数量的参数调用函数的方法
2016/05/31 Javascript
浅析如何利用JavaScript进行语音识别
2016/10/27 Javascript
layer弹窗插件操作方法详解
2017/05/19 Javascript
微信分享调用jssdk实例
2017/06/08 Javascript
JS通过位运算实现权限加解密
2018/08/14 Javascript
详解Vue-cli3 项目在安卓低版本系统和IE上白屏问题解决
2019/04/14 Javascript
vue transition 在子组件中失效的解决
2019/11/12 Javascript
javascript实现画板功能
2020/04/12 Javascript
vue实现禁止浏览器记住密码功能的示例代码
2021/02/03 Vue.js
[01:24:51]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS NewBee第二场
2014/05/26 DOTA
[03:48]大碗DOTA
2019/07/25 DOTA
python中将阿拉伯数字转换成中文的实现代码
2011/05/19 Python
浅谈Python中的数据类型
2015/05/05 Python
详解MySQL数据类型int(M)中M的含义
2016/11/20 Python
如何打包Python Web项目实现免安装一键启动的方法
2020/05/21 Python
用python制作个音乐下载器
2021/01/30 Python
加拿大著名时装品牌:SOIA & KYO
2016/08/23 全球购物
纽约通行卡:The New York Pass(免费游览纽约90多个景点)
2017/07/29 全球购物
ghd法国官方网站:英国最受欢迎的美发工具品牌
2019/04/18 全球购物
全球性的在线商店:Vogca
2019/05/10 全球购物
汇科协同Java笔试题
2012/03/31 面试题
导游个人求职信
2014/04/25 职场文书
会计个人实习计划书
2014/08/15 职场文书
自查自纠工作总结
2014/10/15 职场文书
2015年行政人事工作总结
2015/05/21 职场文书
《中彩那天》教学反思
2016/02/24 职场文书
祝福语集锦:给妹妹结婚的祝福语
2019/12/18 职场文书
解决numpy数组互换两行及赋值的问题
2021/04/17 Python
Springboot-cli 开发脚手架,权限认证,附demo演示
2022/04/28 Java/Android