我们如何在expressjs服务器上使用body-parser从表单中获取单选按钮值? [英] How can we get radio button values from form using body-parser on an expressjs server?

查看:77
本文介绍了我们如何在expressjs服务器上使用body-parser从表单中获取单选按钮值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中一部分询问用户是男性还是女性.这是html

I have a form and part of it asks the user if they are male or female. Here is the html,

<form role="form" id="createAccountForm" action="CAform" method="POST">
.
.
.
.
<div class="form-group" id="gender">
<label class="radio-inline"><input type="radio" name="optradio" value="1">Male</label>
<label class="radio-inline"><input type="radio" name="optradio" value="0">Female</label>
</div>
.
.
.
</form>

在服务器(具有快速框架的节点服务器)上,我想知道当用户提交表单时如何访问性别值.你可以用人体分析仪吗?

and on the server (a node server with express framework), I want to know how to access the gender value when the user submits the form. Can you do it with body-parser?

推荐答案

您只需要设置bodyParser的中间件就可以了.

You just need to set the middlewares of bodyParser and you're good to go.

var bodyParser = require('body-parser'); 
app.use(bodyParser.json()); // to support JSON bodies
app.use(bodyParser.urlencoded({ extended: true })); // to support URL-encoded bodies

app.post('/CAForm', function(req, res) {
  res.send(req.body.optradio);
});

这篇关于我们如何在expressjs服务器上使用body-parser从表单中获取单选按钮值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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