我正在学习 Node.js 和 Express 框架的基础知识。
我有一个带有两个按钮的简单页面:
<form action="/home2" method="post">
<button name="butt1">butt1</button>
<button name="butt2">butt2</button>
</form>
我想在控制台中查看按下了哪个按钮:
router.post('/', function(req, res, next) {
console.log(req.body.name);
res.render('home2', { title: 'post' });
});
在控制台中我只看到
undefined
如何访问按钮的名称?
我想这对你会有帮助。
<form action="/home2" method="post">
<button name="butt1">butt1</button>
<button name="butt2">butt2</button>
</form>
router.post('/home2', function(req, res, next) {
if(req.body.hasOwnProperty("butt1")){
console.log("butt1 clicked");
}else{
console.log("butt2 clicked");
}
res.render('home2', { title: 'post' });
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)