首页 > 解决方案 > 如何仅在节点 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);

标签: javascripthtmlnode.jsejs

解决方案


你是什​​么意思“当程序运行时”?您的代码在 app.post() 函数中,因此只能在发布表单时触发,而不是在显示页面时触发。

似乎其他东西正在发布表单,或者您有一段代码自动发布。


推荐阅读