如何将表单的checkbox分组到Node中的数组中?
我正在尝试用多个checkbox创build一个表单。 我想每个checkbox共享相同的名称,并检查的价值与表格提交。 我知道这可以用PHP来完成(看到这个问题 ),但我不知道如何使用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[]')
访问值,但是它们都返回undefined。
如果你改变你的app.js
这一行:
app.use(bodyParser.urlencoded({ extended: false }));
至:
app.use(bodyParser.urlencoded({ extended: true }));
你应该用req.param('section')
得到你所期待的。