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 相关文章推荐
屏蔽Flash右键信息的js代码
Jan 17 Javascript
JavaScript toFixed() 方法
Apr 15 Javascript
基于jquery的修改当前TAB显示标题的代码
Dec 11 Javascript
如何使用HTML5地理位置定位功能
Apr 27 Javascript
JS图片定时翻滚效果实现方法
Jun 21 Javascript
Bootstrap标签页(Tab)插件使用方法
Mar 21 Javascript
JS表格组件神器bootstrap table使用指南详解
Apr 12 Javascript
从零开始学习Node.js系列教程一:http get和post用法分析
Apr 13 Javascript
原生js实现仿window10系统日历效果的实例
Oct 31 Javascript
vue将对象新增的属性添加到检测序列的方法
Feb 24 Javascript
vue左侧菜单,树形图递归实现代码
Aug 24 Javascript
vue添加axios,并且指定baseurl的方法
Sep 19 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
php数组函数序列之array_search()- 按元素值返回键名
2011/11/04 PHP
PHP网站开发中常用的8个小技巧
2015/02/13 PHP
php 可变函数使用小结
2018/06/12 PHP
php curl发送请求实例方法
2019/08/01 PHP
ExtJs中简单的登录界面制作方法
2010/08/19 Javascript
Javascript事件热键兼容ie|firefox
2010/12/30 Javascript
JQuery实现鼠标滚轮滑动到页面节点
2015/07/28 Javascript
DOM事件阶段以及事件捕获与事件冒泡先后执行顺序(图文详解)
2015/08/18 Javascript
AngularJS 过滤器的简单实例
2016/07/27 Javascript
js实现图片360度旋转
2017/01/22 Javascript
利用策略模式与装饰模式扩展JavaScript表单验证功能
2017/02/14 Javascript
jQuery插件HighCharts实现的2D面积图效果示例【附demo源码下载】
2017/03/15 Javascript
JS 验证密码 不能为空,必须含有数字、字母、特殊字符,长度在8-12位
2017/06/21 Javascript
js图片轮播插件的封装
2017/07/21 Javascript
Angular2里获取(input file)上传文件的内容的方法
2017/09/05 Javascript
React实践之Tree组件的使用方法
2017/09/30 Javascript
在Vue中使用highCharts绘制3d饼图的方法
2018/02/08 Javascript
Mac下通过brew安装指定版本的nodejs教程
2018/05/17 NodeJs
iview在vue-cli3如何按需加载的方法
2018/10/31 Javascript
jQuery实现的五星点评功能【案例】
2019/02/18 jQuery
Vue keepAlive 数据缓存工具实现返回上一个页面浏览的位置
2019/05/10 Javascript
Vue数据驱动表单渲染,轻松搞定form表单
2019/07/19 Javascript
Threejs实现滴滴官网首页地球动画功能
2020/07/13 Javascript
js实现简易计算器小功能
2020/11/18 Javascript
python转换字符串为摩尔斯电码的方法
2015/07/06 Python
pytorch逐元素比较tensor大小实例
2020/01/03 Python
Win10环境中如何实现python2和python3并存
2020/07/20 Python
会计找工作求职信范文
2013/12/09 职场文书
英语专业学生个人求职信范文
2014/01/06 职场文书
领导干部群众路线教育实践活动剖析材料
2014/10/10 职场文书
幼儿园班级工作总结2015
2015/05/25 职场文书
焦点访谈观后感
2015/06/11 职场文书
php中配置文件保存修改操作 如config.php文件的读取修改等操作
2021/05/12 PHP
Python办公自动化之教你如何用Python将任意文件转为PDF格式
2021/06/28 Python
Javascript 解构赋值详情
2021/11/17 Javascript
js中Map和Set的用法及区别实例详解
2022/02/15 Javascript