Jquery easyui异步提交表单的两种方式

这篇文章分享一下easyui常用的两种表单异步提交的方式。

目录

开始前的准备工作

[方式一:利用jquery ajax提交](#方式一:利用jquery ajax提交)

[.post()](#.post())

[.ajax()](#.ajax())

方式二:使用easyui提供的表单提交方式


开始前的准备工作

1、使用HBuilderX创建一个简单的html项目,删除img和html目录,只保留js目录和index.html;

2、下载jquery.min.js和jquery.easyui.min.js,复制到js目录下;

3、修改index.html的代码,增加一个表单,包含三个输入框和一个提交按钮,在页面引入easyui的js文件;

页面效果如下:

方式一:利用jquery ajax提交

这种方式只需要引入jquery.min.js

$.post()

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>easyui异步提交表单</title>
		<script src="js/jquery.min.js"></script>
	</head>
	
	<body>
		<form id="ajax_form">
			姓名:<input id="name" />
			年龄:<input id="age" />
			手机号:<input id="phone" />
			
			<button type="button" id="submit">提交</button>
		</form>
		
		<script>
			$(function(
				$("#submit").click(function() {
					$.post("/xxx/save", {
						name: $("#name").val(),
						age: $("#age").val(),
						phone: $("#phone").val()
					}, function(resp) {
						// 处理响应的数据
					}, "json");
				});
				
			));
		</script>
	</body>
</html>

$.ajax()

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>easyui异步提交表单</title>
		<script src="js/jquery.min.js"></script>
	</head>
	
	<body>
		<form id="ajax_form">
			姓名:<input id="name" />
			年龄:<input id="age" />
			手机号:<input id="phone" />
			
			<button type="button" id="submit">提交</button>
		</form>
		
		<script>
			$(function(	
				$("#submit").on("click", function() {
					$.ajax({
						url: "/xxx/save",
						type: "post",
						data: {
							name: $("#name").val(),
							age: $("#age").val(),
							phone: $("#phone").val()
						},
						async: true,
						cache: false,
						dataType: "json",
						processData: true,
						success: function(resp) {
							// 处理成功的响应
						},
						error: function(resp) {
							// 处理失败的响应
						}
					});
				});
				
			));
		</script>
	</body>
</html>

方式二:使用easyui提供的表单提交方式

easyui官网已经介绍了这种方式,不过和上面的ajax提交不一样,这种提交方式要给输入框设置name属性。

案例代码:

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>easyui异步提交表单</title>
		<script src="js/jquery.min.js"></script>
		<script src="js/jquery.easyui.min.js"></script>
	</head>
	
	<body>
		<form id="ajax_form" method="post">
			姓名:<input name="name" />
			年龄:<input name="age" />
			手机号:<input name="phone" />
			
			<button type="submit">提交</button>
		</form>
		
		<script>
			let requestUrl = "/xxx/save";
						
			$(document).ready(function() {
				$("#ajax_form").form({
					url: requestUrl,
					success: function(resp) {
						// 处理成功的响应
					}
				});
			
			});
		</script>
	</body>
</html>

好了,文章就分享到这里了,看完不要忘了点赞+收藏哦~

相关推荐
Charlie_lll23 分钟前
学习Three.js–纹理贴图(Texture)
前端·three.js
yuguo.im29 分钟前
我开源了一个 GrapesJS 插件
前端·javascript·开源·grapesjs
安且惜29 分钟前
带弹窗的页面--以表格形式展示
前端·javascript·vue.js
GISer_Jing2 小时前
AI驱动营销:业务技术栈实战(From AIGC,待总结)
前端·人工智能·aigc·reactjs
GIS之路3 小时前
GDAL 实现影像裁剪
前端·python·arcgis·信息可视化
AGMTI3 小时前
webSock动态注册消息回调函数功能实现
开发语言·前端·javascript
不会Android的潘潘4 小时前
受限系统环境下的 WebView 能力演进:车载平台 Web 渲染异常的根因分析与优化实践
android·java·前端·aosp
建军啊4 小时前
java web常见lou洞
android·java·前端
阳无4 小时前
宝塔部署的前后端项目从IP访问改成自定义域名访问
java·前端·部署
Galloping-Vijay4 小时前
解决 WSL2 + Windows Hosts + 开启 VPN 后无法访问本地 Web 服务的问题
前端·windows