使用nodejs捕获复选框的状态
我有配置文件。config.json
{
"checkFlag":"1"
}
我使用nodejs阅读,以设置checkBox的初始值并按如下方式提供网页:
app.js
const express = require('express')
const app = express()
var path =require('path')
var fs = require('fs')
var os = require( 'os' );
var config_fileName = './public/scripts/config.json';
const router = express.Router();
var configFile = require(config_fileName);
app.use(express.static('public'))
var ejs = require('ejs');
var bodyParser = require('body-parser');
app.use(express.urlencoded())
app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'public/views'));
app.set('view engine', 'ejs');
let rawdata = fs.readFileSync(config_fileName);
let checkflag = JSON.parse(rawdata);
console.log(checkflag.checkFlag);
// Serving index.html
app.engine('html', require('ejs').renderFile);
app.get('/', (req,res,next) => {
res.render('index', {
checkvalue:"checked"});
res.end();
});
app.post('/check',(req,res)=>{
console.log('check')
})
app.listen(3000)
并且html代码如下:
**index.ejs**
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>check Box</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
</head>
<body>
<p> hey</p>
<form action="/check" method="post">
<input type="checkbox" name="example" <%=checkvalue%>>reading flag <%=checkvalue%> <br>
</form>
</body>
</html>
阅读效果很好,但是我需要重写配置文件,因为该框处于选中状态/未选中状态,因为我尝试过使用表单,但显然无法正常工作。任何想法如何使用NodeJS做到这一点?
预先感谢!
回答如下:为了阅读,您可以直接执行此操作