[英]Requesting a variable from html to node js
I am currently using express to submit my form. 我目前正在使用Express提交我的表格。 In my html, I have a 20 inputs with different ids.
在我的HTML中,我有20个具有不同ID的输入。 The ids for the inputs are address1,address2,address3... I am calling the variables from node js by using req.body.address1.
输入的ID为address1,address2,address3 ...我正在使用req.body.address1从节点js调用变量。 The client has the option of either filling out all 20 inputs or only one or etc. I want to see outputs in my console using node js.
客户端可以选择填写全部20个输入,也可以仅填写一个或多个。我想使用node js在控制台中查看输出。 Is there any way I can do this without typing
console.log(req.body.address)
for each variable. 有什么办法可以做到而无需为每个变量键入
console.log(req.body.address)
。 Is there a way you can say req.body.address of a count
? 有没有办法说出
req.body.address of a count
? Please comment if any confusions occurs 如果有任何混淆,请发表评论
You can use address[]
as variable name in HTML. 您可以在HTML中使用
address[]
作为变量名。 So req.body.address
will be array and one console.log
will be require. 因此
req.body.address
将是数组,并且需要一个console.log
。
Or use some js-magic 或使用一些js-magic
console.log(Object.keys(req.body).reduce(function(r, k) {if (k.indexOf('address') == 0) r[k] = req.body[k]; return r; }, {}))
Object.keys
returns all key from body, then reduce
walk by them and put req.body[k]
with key k
who started from "address" (check by .indexOf('address') == 0
) to result object. Object.keys
从body返回所有键,然后reduce
它们的走动,然后将req.body[k]
与从“地址”(由.indexOf('address') == 0
)开始的键k
.indexOf('address') == 0
以生成对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.