【问题标题】:Post then change text in html nodejs发布然后更改html nodejs中的文本
【发布时间】:2021-11-19 01:47:33
【问题描述】:

我在 Html 中有一个表单标签:

app.get("/",(req,res)=>{
res.sendFile(__dirname+"/index.html")

})

app.post("/Cong",urlencodedParser,(req,res)=>{
    var a = parseInt(req.body.a);
    var b = parseInt(req.body.b);
    var c = a + b;
    res.send("Result: "+c)//This is will route to a new website, but I just want change text in my HTML
})
 <form action="/Cong" method="post">
  A: <input type="text" name="a"><br>
  B: <input type="text" name="b"><br>
<label id="result"></label>
</form>

当我单击按钮时,如何在文件 js 中使用 id result = value C 更改 HTML 中的标签。

【问题讨论】:

    标签: javascript html node.js web post


    【解决方案1】:

    看着你的代码,我对你试图在其中实现这一点的上下文有点困惑。

    在浏览器中,你会做document.getElementById("result").innerHTML = c

    但是你不显示前端代码,只显示后端,你需要从前端调用你的后端端点

    例如:发帖到 /Cong 这是 Axios 中的一个示例

    axios.post('/Cong', {
        a: 1,
        b: 2
      })
      .then(function (response) {
           document.getElementById("result").innerHTML = response;
      })
      .catch(function (error) {
        console.log(error);
      });
    

    你的后端res.send("Result: "+c) 也应该是res.send(c),否则前端会得到“Result: 3” 而不仅仅是 3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      相关资源
      最近更新 更多