我有一个文本类型的输入标签。我想在用户输入时访问输入标记的值并将其推送到 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/