我正在尝试创建一个带有多个复选框的表单。我希望每个复选框都具有相同的名称,并且选中的复选框的值将与表单一起提交。我知道这可以用 PHP 来完成(参见 this SO question ),但我不知道如何用 Node.js 和 Express 来完成。这是我的 HTML 的修改版本:
<label><input type="checkbox" name="section[]" value="1" />Item Name</label>
<label><input type="checkbox" name="section[]" value="2" />Item Name</label>
<label><input type="checkbox" name="section[]" value="3" />Item Name</label>
当我处理 POST 请求时,我尝试使用 req.param('section')
访问值和 req.param('section[]')
但它们都返回未定义。
最佳答案
如果您在 app.js
中更改此行:
app.use(bodyParser.urlencoded({ extended: false }));
到:
app.use(bodyParser.urlencoded({ extended: true }));
您应该通过 req.param('section')
得到您期望的结果。
关于html - 如何将表单的复选框分组到 Node 中的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27327178/