Node.js处理选择表单元素

编程入门 行业动态 更新时间:2024-10-26 09:28:08
本文介绍了Node.js处理选择表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个(可能非常愚蠢的)问题. 我有使用Express提交到我的nodeJS服务器的表单. 这与文本输入和单选按钮完美配合,但是现在我必须添加一个选择. 服务器没有给出错误,但是选择没有被正确解析.

I have a (probably very stupid) question. I have a form I submit to my nodeJS server with Express. This works perfectly with text inputs and radiobuttons, but now I have to add a select. The server does not give an error but the select is not parsed properly.

我的代码:

<select id="chooselang"> <option value="nl" name="language">NL</option> <option value="en" name="language">EN</option> </select>

我的服务器如下:

app.post('/settings', function(req, res){ // Fill JSON array with new settings var myData = { ,name : req.body.name ,mail : req.body.email ,language : req.body.language ,location: req.body.location } // Write to JSON file fs.writeFile(configfilepath, JSON.stringify(myData, null, 4), function(err) { if(err) { res.send(500); console.log(err); } else { setTimeout(function () { res.redirect('back'); }, 2000) } }); });

有人可以告诉我我在做什么错吗?

Could someone please tell me what I'm doing wrong?

推荐答案

可能您需要在select中添加一个name属性,并使用该属性捕获值.

Probably you will need to add a name attribute in your select and use that to capture the values.

此外,仅option值之一将被发送到服务器,因此,如果这是您的意图,则将name分配给每个选项都没有意义.

Also, only one of the option values will be sent to the server, so there is no point in assigning name to each one of the options, if that was your intention.

更多推荐

Node.js处理选择表单元素

本文发布于:2023-08-07 06:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   元素   Node   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!