javascript - 如何知道按下了哪个按钮?

标签 javascript node.js button express body-parser

我正在学习 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/

相关文章:

android - map Activity 上的自定义按钮

javascript - 如何使用 javascript/条件注释测试浏览器是否符合 css3?

javascript - 将页面对象模型与 Protractor 一起使用时,Promise 未得到解决

javascript - console.log(util.format 运行错误

node.js - 如何使用 Node.js 代码获取所有 mongoDB 集合名称?

Android:共享首选项不保存按钮的状态

Javascript三 Angular 形确定

javascript - 如果使用特殊字符存储值,则使用 $regex 的 Mongoose 查找不会返回预期结果

node.js - 可以使用 Node.js 构建 FormFlow 对话框吗?

c - 修改几个GTK按钮