如何知道按下了哪个按钮? [英] How to know which button was pressed?

查看:93
本文介绍了如何知道按下了哪个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习node.js和express框架的基础知识. 我有一个带有两个按钮的简单页面:

I'm learning the basics of node.js and express framework. I have a simple page with two buttons:

<form action="/home2" method="post">
    <button name="butt1">butt1</button>
    <button name="butt2">butt2</button>
</form>

我想在控制台中查看按下了哪个按钮:

And i want to see in console which button was pressed:

router.post('/', function(req, res, next) {
    console.log(req.body.name);
    res.render('home2', { title: 'post' });
});

在控制台中,我只是看到

In the console i just see

undefined

如何访问按钮的名称?

推荐答案

首先,因为您使用的是POST,所以我假设您已经有了body-parser中间件,如果未选中

First of all since you are using POST i am assuming you've got the body-parser middleware present, if not check Body Parser Middleware

您的代码需要进行一些更改

your code needs a few changes

以html

<form action="/home2" method="post">
    <button name="butt" value='1'>butt1</button>
    <button name="butt" value='2'>butt2</button>
</form>

速递

router.post('/home2', function(req, res, next) {
    console.log(req.body.butt);
    res.render('home2', { title: 'post' });
});

req.body.name必须为req.body.butt

/必须为/home2

这篇关于如何知道按下了哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆