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>

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

相关推荐
你的人类朋友6 小时前
✍️记录自己的git分支管理实践
前端·git·后端
合作小小程序员小小店6 小时前
web网页开发,在线考勤管理系统,基于Idea,html,css,vue,java,springboot,mysql
java·前端·vue.js·后端·intellij-idea·springboot
防火墙在线6 小时前
前后端通信加解密(Web Crypto API )
前端·vue.js·网络协议·node.js·express
Jacky-0086 小时前
Node + vite + React 创建项目
前端·react.js·前端框架
CoderYanger7 小时前
前端基础——CSS练习项目:百度热榜实现
开发语言·前端·css·百度·html·1024程序员节
i_am_a_div_日积月累_7 小时前
10个css更新
前端·css
倚栏听风雨8 小时前
npm命令详解
前端
用户47949283569158 小时前
为什么我的react项目启动后,dom上的类名里没有代码位置信息
前端·react.js
键盘飞行员8 小时前
Vue3+TypeScript项目中配置自动导入功能,遇到了问题需要详细的配置教程!
前端·typescript·vue
han_8 小时前
前端高频面试题之Vue(初、中级篇)
前端·vue.js·面试