您可以使用括号符号来访问它
fields['hob[]']
。
Sample form:
<!-- RADIO BUTTONS -->
<p>
<input class="with-gap" type="radio" name="gender" id="male" value="M" checked>
<label for="male">Male</label>
</p>
<p>
<input class="with-gap" type="radio" name="gender" id="female" value="F">
<label for="female">Female</label>
</p>
<button type="submit" class="btn ">Sign up</button>
要从复选框中提取我们的值,我们可以通过 req.body.gender 来实现,M/F 的值将存储在 person 对象中
let person = {
gender: req.body.gender
}
在
.ejs
文件中,我们将遵循。
<form action="/addhobby" method="POST">
<div class="form-group">
<h5>Hobbies</h5>
<input type="checkbox" id="hobby" name="stuhobbies" value="sing"/> <strong>Sing</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="guitarplay"/> <strong>GuitarPlay</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="cricket"/> <strong>Cricket</strong>
<input type="checkbox" id="hobby" name="stuhobbies" value="football"/> <strong>Football</strong>
</div>
<button type="submit" class="btn btn-primary btn-md">
Add
</button>
现在在操作或路线处理文件中我们执行以下操作。
const router = express.Router();
router.post('/addhobby', ensureAuthenticated,(req, res) => {
const { stuhobbies } = req.body;
console.log(stuhobbies);
});