Node.js 将量程值提交给控制器
Node.js submit span values to controller
如何将 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){
});
});
});
<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 然后从服务器检索 cookie
如何将 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){
});
});
});
<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 然后从服务器检索 cookie