如何在Node应用程序中获取选定的下拉列表值

时间:2016-03-09 04:15:01

标签: javascript node.js drop-down-menu

使用车把home.handlebars将性别下拉菜单作为注册表单的一部分......

<form id="register-modal" class="ui modal form" method="post" action="/register">
  <div class="field">
    <label>Username</label>
    <input type="text" name="username" placeholder="Username">
  </div>
  <div class="field">
  <div class="two fields">
    <div class="field">
      <label>Age</label>
      <input type="text" name="age">
    </div>
    <div class="field">
      <label>Gender</label>
      <select class="ui fluid dropdown">
        <option value="male">Male</option>
        <option value="female">Female</option>
        <option value="non-binary">Non-binary</option>
      </select>
    </div>
  </div>
</div>
 <div class="field">
    <label>Email Address</label>
    <input type="text" name="email" placeholder="Email Address">
  </div>
  <div class="field">
    <label>Password</label>
    <input type="password" name="password" placeholder="Password">
  </div>
  <center><button class="ui button" type="submit">Submit</button></center>
</form>

我有一个创建像这样的新用户对象的函数......

function user(name, pass) {
   return {
      name: name,
      pass: pass
   };
}  

以下是我如何从表单中检索信息。这是routes.js

router.post('/register', (req,res) => {
var name = req.body.username;
var pass = req.body.password;
var email = req.body.email;

if (!name || !pass || !email ) {
  res.redirect('registration?code=fail');
} else{
db.insertUser(creations.user(name,pass,email,false),(user) => {
  if(user){
    db.login(user,(sessionID)=>{
      res.cookie('sessionID', sessionID);
      res.redirect('/feed');
    })
  } else res.redirect('registration?code=IU');
})
}})

1 个答案:

答案 0 :(得分:1)

我明白了。要从下拉列表中获取所选值,我只需调用:

var gender = req.body.gender