<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FormData 表单序列化</title>
</head>
<body>
<form id="form">
<div>
<label for="username">用户名:</label>
<input type="text" name="username" />
</div>
<div>
<label for="password">密码:</label>
<input type="password" name="password" />
</div>
<div>
<button id="btn">发送</button>
</div>
</form>
<script>
const btn = document.getElementById("btn");
btn.addEventListener("click", function (e) {
e.preventDefault();
const form = document.getElementById("form");
const formData = new FormData(form);
for(const [key,value] of formData){
console.log(key,value);
}
});
</script>
</body>
</html>