Node.js处理选择表单元素 [英] nodejs handeling a select form element

查看:180
本文介绍了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处理选择表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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