我正在将网页值插入 Mongo DB,我使用的是 Node JS + Express + Jade。 我需要插入 True 或 False,具体取决于复选框是否启用。
在我的 router.js 中,我有以下内容:
app.get('/notification', function(req, res) {
res.render('notification', { title: 'Nueva notificacion', notifications : NT });
});
app.post('/notification', function(req, res){
console.log('Adding new notification');
AM.addNewNotification({
name : req.param('name'),
notification : req.param('notification'),
active : req.param('active')
}, function(e){
if (e){
res.send(e, 400);
} else{
res.send('ok', 200);
}
});
});
在我看来,我有以下几点:
.controls
label#active-me.checkbox Active
input(type="checkbox", name="active", id="active",checked='checked')
但我不能插入它,看起来没有取值。 仅插入名称和通知值(文本)。
{ "name" : "Goodbye world", "notification" : "Short Message Service", "_id" : ObjectId("5298f603bd07b80376000005") }
有什么建议吗?
谢谢
最佳答案
当复选框被选中时,浏览器会将复选框的值添加到请求中。有几种方法可以在服务器上处理此行为。
最明显的是将参数不存在视为其为假。
另一种方法是 Rails 所做的:在复选框前添加一个具有相同名称和零值的隐藏输入。在你的情况下:
.controls
label#active-me.checkbox Active
input(type="hidden", name="active", value=0)
input(type="checkbox", name="active", id="active",checked='checked')
此外,您在 app.post
中的代码可能应该使用 req.body
,而不是 req.param
。
关于node.js - 读取复选框值 Node JS + Express + Jade,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20965775/