如何解决如何使按钮仅在节点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()函数中,因此只能在发布表单时(而不是在显示页面时)触发。
似乎其他情况正在发布表单,或者您有一段代码自动发布。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。