Javascript/Node/EJS 中用于发布的多个提交按钮
因此,我正在学习JavaScript,而我正在练习的一件事是各种模型视图控制器。目前,我有一个具有多个EJS模板的表格,但只有第一个与此问题有关。
第一个是家。
<DOCTYPE html>
<html>
<head>
<title> HomeTitle </title>
<style>
body{background: skyblue;}
</style>
<body>
<p> Welcome Home</p>
<div>
<form id ="redirect" method="POST">
<input type="submit"value="SignIn" >
<input type="submit" value="Exit" >
</form>
</body>
<script>
</script>
</head>
</html>
我也有一个路由器。将作为路由器文件处理。
// Importing the module
const express=require("express")
var bodyParser=require('body-parser')
// Creating express Router
const router=express.Router()
var jsonParser= bodyParser.json()
var urlencodedParser =bodyParser.urlencoded({extended: false});
// Handling login request
router.get("/",(req,res,next)=>{
res.render('home.ejs')
})
router.post("/", urlencodedParser, function (req,res){
return res.redirect("/login");
})
router.get("/login",(req,res,next)=>{
res.render("profile.ejs")
})
router.post("/login", urlencodedParser, function (req,res){
console.log(req.body)
//res.send(req.body.first)
return res.redirect('/');
})
module.exports=router
在我的家中。ejs我有两个提交按钮,一个用于登录,一个用于退出。目前,按两个按钮将我带到页面中的符号(profile.ejs)我正在渲染(b/c只有一个渲染目的地)。有没有办法让我区分Javscript部分中的哪个按钮,以便我可以选择要呈现的页面。例如,按下符号按钮将渲染一个EJS模板,而退出将转到我将渲染的另一个模板。
第一个路由器。post是邮政功能,理想情况下,区分解决方案是因为我根据帖子数据处理新页面的地方。
So I'm learning Javascript and one of the things I'm practicing is a model view controller of sorts. Right now I have a form with multiple ejs templates, but only the first one pertains to this question.
The first is Home.ejs
<DOCTYPE html>
<html>
<head>
<title> HomeTitle </title>
<style>
body{background: skyblue;}
</style>
<body>
<p> Welcome Home</p>
<div>
<form id ="redirect" method="POST">
<input type="submit"value="SignIn" >
<input type="submit" value="Exit" >
</form>
</body>
<script>
</script>
</head>
</html>
I also have a router.js that handles as the router file.
// Importing the module
const express=require("express")
var bodyParser=require('body-parser')
// Creating express Router
const router=express.Router()
var jsonParser= bodyParser.json()
var urlencodedParser =bodyParser.urlencoded({extended: false});
// Handling login request
router.get("/",(req,res,next)=>{
res.render('home.ejs')
})
router.post("/", urlencodedParser, function (req,res){
return res.redirect("/login");
})
router.get("/login",(req,res,next)=>{
res.render("profile.ejs")
})
router.post("/login", urlencodedParser, function (req,res){
console.log(req.body)
//res.send(req.body.first)
return res.redirect('/');
})
module.exports=router
In my Home.ejs I have two submit buttons, one for signing in, and one for exiting. At present, pressing either button takes me to the sign in page (profile.ejs) I am rendering (b/c there's only one render destination). Is there a way for me to differentiate which button was pressed in the javscript section so that I can chose which page I want to render. For example pressing the Sign In button would render one ejs template, whereas Exit would go to another template that I would render.
The first router.post is the post function where ideally the solution for differentiating would be since that is where I handle rendering the new page based on the post data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为输入指定名称:
然后检查提交的输入的值并根据该值进行渲染/重定向:
You can assign a name to your inputs:
Then check the value of submitted input and render/redirect based on that value: