Node.js将span值提交给controller

时间:2017-06-13 08:52:39

标签: javascript node.js express form-submit

如何将span标签中的文本值发送到控制器?

user_profile.hbs

<div class="panel panel-default">
  <div class="panel-heading">User Profile</div>
  <div class="panel-body">
    <form method="get" enctype="multipart/form-data" action="/home/user/profile/update">
      <div>
        Username: <span id="user_upd">{{user}}</span>
      </div>
      <div>
        Email: <span id="email_upd">{{email}}</span>
      </div>
      <div>
        Type: <span>{{type}}</span>
      </div>
    </div>
    <input type="submit" value="Update Info"/>
  </form>
</div>

userController.js

// Display User update form on GET
module.exports.userUpdateGet = function(req, res) {
  console.log(req.body);
  res.render('user_profile_update', {
    usename: req.body.username,
    email: req.body.email,
    user: req.user.username
  });  
}

这是user_profile_update视图:

<div class="panel panel-default">
  <div class="panel-heading">Update User</div>
  <div class="panel-body">
    <form method="post" enctype="multipart/form-data" action="/home/user/profile/update">
      <div>
        <label for="username">Username: </label>
        <input type="text" name="username" id="username" value={{username}}/>
      </div>
      <div>
        <label for="email">Email: </label>
        <input type="text" name="email" id="email" value={{email}}/>
      </div>
    </div>
    <input type="submit" value="Save Changes"/>
  </form>
</div>

我的目标是将user_profile.hbs的信息显示为user_profile_update.hbs的输入值。

编辑:

我尝试使用此js代码,但user_profile_update视图未呈现...为什么?

  $(document).ready(function(){
    var user,email;
    $("#submit_update").click(function(){
      user= $("#user_upd").text();
      email= $("#email_upd").text();
      $.get("/user/profile/update", {user: user,email: email}, function(data){
      });
    });
  });

1 个答案:

答案 0 :(得分:0)

<div class="panel panel-default">
  <div class="panel-heading">User Profile</div>
  <div class="panel-body">
    <form method="get" enctype="multipart/form-data" action="/home/user/profile/update">
      <div>
        Username: <span>{{user}}</span>
      </div>
      <div>
        Email: <span>{{email}}</span>
      </div>
      <div>
        Type: <span>{{type}}</span>
      </div>
    </div>
    <input type="submit" value="Update Info"/>
  </form>
</div>

将此更改为

<div class="panel panel-default">
  <div class="panel-heading">User Profile</div>
  <div class="panel-body">
    <form method="get" enctype="multipart/form-data" action="/home/user/profile/update">
      <input type="hidden" name="user" value={{user}}/>
      <input type="hidden" name="email" value={{email}}/>
      <input type="hidden" name="type" value={{type}}/>
      <div>
        Username: <span>{{user}}</span>
      </div>
      <div>
        Email: <span>{{email}}</span>
      </div>
      <div>
        Type: <span>{{type}}</span>
      </div>
    </div>
    <input type="submit" value="Update Info"/>
  </form>
</div>

您可以使用input hidden或在表格上点击时可以使用js设置Cookie&amp;然后从服务器

中检索cookie