js object notation in html input name

时间:2016-10-20 19:26:21

标签: javascript html html5

I am using server side javascript with node.js. Is it possible to make a form input name in html like this:

<input name="name.first">
<input name="name.last">

to get this js object on server side?

req.body = {
    name: {
        first: "firstname",
        last: "lastname"
    }
}

Actually I get not the "name" object with children "first" and "last", but:

'realname.first': 'firstname',
'realname.last': 'lastname',
...

Problem of node.js??

SOLUTION:

Like Eero Otsus said, I had to set brackets, not a period. For example:

<input name="name[first]">

Thanks a lot.

1 个答案:

答案 0 :(得分:0)

我在使用 node 和 mongoose 时遇到了类似的问题。我的用户控制器正在与我的表单作斗争。我最终离开了表单 < input name="name" > 用于名字和姓氏输入。这将它们作为值 0 和 1 放在“名称”数组中。然后我将控制器更改为 姓名:{第一个:req.body.name[0],最后一个:req.body.name[1]}。问题已解决。