I'm using Node+Express+Jade to render some webpages. On a form there are 2 checkboxes. When the form is submitted through POST, if the checkbox is checked, I get req.body.checkbox1 -> 'on'
, if isn't checked, I get req.body.checkbox1 -> undefined
Is possible to get checkbox value as true
or false
?
Here's my server side test code
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/webroot'));
app.set('views', __dirname + '/view');
app.set('view engine', 'jade');
app.listen(3001);
app.get('/', function (req, res) {
res.render('test');
});
app.post('/config', function (req, res) {
console.log(req.body.t);
console.log(req.body.f);
res.redirect('/');
});
And my Jade form test
form(action='/config', method='post')
input(type="checkbox", name="not_checked", checked=false)
input(type="checkbox", name="checked", checked=true)
input(type='submit')