javascript - 如何仅在节点 js 和 ejs 中单击时使按钮提交值
问题描述
我在 ejs 文件中有一个按钮,我希望它只在单击时向 app.js 提交值,但现在它在程序运行时立即发送值。
<% notesArray.forEach((note,i) =>{ %>
<div class="note">
<h3> <%= note.title %> </h3>
<p> <%= note.text %> </p>
<form class="" action="/" method="post">
<button type="submit" name="deletebutton" class="deleteButton" value=<%-note.title-%>><i class="fa fa-trash"></i></button>
</form>
</div>
<% }); %>
在这里,我有这个变量“submitFromButton”,它在程序运行时立即获取一个值,但我只希望在使用单击上面提到的 ejs 文件中的按钮时从中获取值
app.post("/",function(req,res){
var note = {
title:req.body.inputTitle,
text:req.body.inputText,
inputValue:req.body.submitButton
}
if (note.inputValue == 'note'){
notesArray.push(note);
}
var submitFromButton = req.body.deletebutton; // here is the problem
console.log(submitFrombutton);
解决方案
你是什么意思“当程序运行时”?您的代码在 app.post() 函数中,因此只能在发布表单时触发,而不是在显示页面时触发。
似乎其他东西正在发布表单,或者您有一段代码自动发布。
推荐阅读
- ios - 防止 Webkit 视图允许下拉屏幕,在视图顶部露出间隙
- python - 如何在递归函数中将乌龟返回其原点
- ms-access - MS Access 在除法时返回错误值
- javascript - 试图通过类名获取多个元素并将它们的值更改为 true 或 false
- reactjs - create-react-app 构建/服务环境变量
- python-3.x - Python:无法从“skimage.color”导入名称“rgb2gray”
- jquery - JQuery .prop(propertyName, value) 在 FireFox 中的 Filelist 时更改值
- python - 检查二维数组中单个单元格周围的单元格时,代码始终为所有单元格返回 1 或 0
- c# - 有没有办法发送 OdbcCommand.ExecuteReader-NonQuery 命令在 API 上执行?
- php - 未针对指定类别 ID 显示自定义帖子类型帖子