admin管理员组文章数量:1323731
I want to get value from a form: Here's the form:
<form method='post' action='/stack'>
<input name="stack0" value="stackoverflow0"/>
<input name="stack1" value="stackoverflow1"/>
<button type='submit'>Click</button>
</form>
if we want to get the value from the form we use:
app.post('/stack',function(req,res){
var tmp = req.body.stack0;
var tmp1 = req.body.stack1;
console.log(tmp) // stackoverflow0
console.log(tmp1) // stackoverflow1
});
I wont use this method because i have a lot of values, i want something like loop,
for(var i=0;i<2;i++){
var tmp = req.body.stack(i); // any syntaxe like that ?
console.log(tmp) // souldstackoverflow0 if i==0, souldstackoverflow1 if i==1
}
when i
take 0; should tmp
take req.body.stack0, and when i==1
tmp = req.body.stack1 ?
help plz, and thnx :)
I want to get value from a form: Here's the form:
<form method='post' action='/stack'>
<input name="stack0" value="stackoverflow0"/>
<input name="stack1" value="stackoverflow1"/>
<button type='submit'>Click</button>
</form>
if we want to get the value from the form we use:
app.post('/stack',function(req,res){
var tmp = req.body.stack0;
var tmp1 = req.body.stack1;
console.log(tmp) // stackoverflow0
console.log(tmp1) // stackoverflow1
});
I wont use this method because i have a lot of values, i want something like loop,
for(var i=0;i<2;i++){
var tmp = req.body.stack(i); // any syntaxe like that ?
console.log(tmp) // souldstackoverflow0 if i==0, souldstackoverflow1 if i==1
}
when i
take 0; should tmp
take req.body.stack0, and when i==1
tmp = req.body.stack1 ?
help plz, and thnx :)
2 Answers
Reset to default 4You have to use the bracket notation to access the object properties if you want to loop over them with a variable in the name:
for(var i=0;i<2;i++){
var tmp = req.body['stack' + i];
console.log(tmp)
}
We have to use BodyParser to grab data from the input field. Code is here HTML code
<form class="" action="/" method="post">
<input type="text" name="newItem" value="" placeholder="Please enter your task">
<button type="submit" name="button">Add</button>
const express = require('express');
const bodyParser = require('body-parser')
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
var temp = req.body.newItem; console.log(temp)
本文标签: javascriptGet value from input in NodeJSStack Overflow
版权声明:本文标题:javascript - Get value from input in NodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122330a2421774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论