【问题标题】:how to tell which button was clicked in post request with node backend如何判断在带有节点后端的发布请求中单击了哪个按钮
【发布时间】:2021-10-06 18:00:37
【问题描述】:

我有一个节点应用程序,在前端,我有多个按钮可以激活下面的 formaction 属性指定的相同发布路由:

<button type="submit" formaction="/specifiedroute">-</button>

但是,我希望能够知道在发布路线中单击了哪个按钮。无论如何我可以访问发布路由中按钮的名称或 id 属性(可能在请求对象中)?如果不是,识别按钮的唯一方法是向 formaction 添加一个参数,如下所示:

<button type="submit" formaction="/specifiedroute?redbutton">-</button>

请注意,所有这些按钮都以一种形式存在(我无法更改),而且我不能只使用隐藏的输入字段。

【问题讨论】:

    标签: node.js post button request htmlbutton


    【解决方案1】:

    也许你应该试试这个

    <button type="submit" formaction="/specifiedroute?button=red">Red</button>
    <button type="submit" formaction="/specifiedroute?button=green">Green</button>
    <button type="submit" formaction="/specifiedroute?button=blue">Blue</button>
    

    // NodeJS

    控制器

    app.post('/specifiedroute', (req, res) => {
      let buttons = {
        "red": "Red Button Clicked",
        "green": "Green Button Clicked",
        "blue": "Blue Button Clicked"
      }
      let reqButton = req.query.button;
      res.send(buttons[reqButton])
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 2012-11-12
      • 2011-02-10
      • 1970-01-01
      • 2011-04-04
      • 2020-07-03
      • 1970-01-01
      相关资源
      最近更新 更多