form跳转提交转ajax提交,使用原生required校验必填数据

代码如下

<html>
	<head>
		<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
	</head>
	<body>
		<form id="form" onsubmit="return sub()">
			<input id="a" name="a" required />
			<input id="b" name="b" required />
			<input id="c" name="c" required />
			<input type="submit" value="提交" />
		</form>
		<script type="text/javascript">
			function sub() {
				var formData = new FormData($("form")[0])
				var data = new Object()
				for (var pair of formData.entries()) {
					data[pair[0]] = pair[1]
				}
				console.log(data)
				$.ajax({
					url: "XXX",
					type: "post",
					data,
				}).then(d => console.log(d)).catch(e => console.log(e.statusText))
				return false;
			}
		</script>
	</body>
</html>

你可能感兴趣的:(form跳转提交转ajax提交,使用原生required校验必填数据)