我正在学习 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' });
});
关于javascript - 如何知道按下了哪个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703145/