javascript - 使用javascript访问jade元素

标签 javascript node.js express pug

我有一个文本类型的输入标签。我想在用户输入时访问输入标记的值并将其推送到 json。

我的 Jade 文件看起来像,

扩展布局

block content
  h1 todos
  #task
  <input type="text" id="task" name="task"/>
  #todos
    for todo in todos
      include todo

我正在使用express编写访问代码,

    app.get('/', function(req,res){
        todos.push(new todo(req.bodyParser.task)) 
        res.render('index',{todos:todos});
});

我也是 javascript、node 和 jade 的初学者。

最佳答案

使用表单将数据提交到服务器:

block content
  h1 todos
  form(action="/todos", method="post", id="taskform")
    input(type="text", name="task")
  #todos
    for todo in todos
      include todo

在 Node 中,现在您可以使用req.body.task访问task:

app.post('/todos', function(req,res){
   todos.push(new todo(req.body.task)) 
   res.render('index',{todos:todos});
});

关于javascript - 使用javascript访问jade元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25915208/

相关文章:

node.js - 如何将 jasmine 与服务器端 typescript 项目一起使用?

javascript - 无法从不同的网络连接 express.js

javascript - 使用 glob 导入多个文件

javascript - 使用 Uniswap SDK 给我这个错误 : resolver or addr is not configured for ENS name

javascript - 尝试使用加载器动画来购买我的网页一些时间来加载,但网页仍然只在动画之后和 block 中加载

javascript - 可以选择一个没有名称属性的 radio 吗?

node.js - Sequelize WHERE 是否强制一个条件取决于编程条件

javascript - 带 Express 的 Aurelia 的最小示例

node.js - Auth0 和 Express-openid-connect/回调没有响应

javascript - Express js redirect with cookie, cookie not present