簡體   English   中英

如何獲取app.js中select輸入的值?

[英]How to get the value of select input in app.js?

我想獲取在 html 中選擇的選項的值並調用一些 api,所以我使用了 req.body 這樣做。 但它在控制台中給出了未定義的。

我已經給出了我的 html 代碼和 app.js 代碼。 請找出錯誤並給我正確的答案。

HTML 代碼(feedback.ejs)

<form method="post" id="tableForm" action="/feedback">
<h1>Select input</h1>
  <select class="example" name="example1">
      <option name="" value="0" selected>Select table</option>
      <option name="table1" value="1">Option 1</option>
      <option name="table2" value="2">Option 2</option>
      <option name="table3" value="3">Option 3</option>
  </select>
  <select class="example" name="example2">
      <option name="" value="0" selected>Select table</option>
      <option name="table1" value="1">Option 1</option>
      <option name="table2" value="2">Option 2</option>
      <option name="table3" value="3">Option 3</option>
  </select>
</form>

Javascript 代碼(app.js)

const express = require("express");
const bodyParser = require("body-parser");

const { response } = require('express');
const { error } = require('console');



const app = express();

app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));


app.get("/feedback", function(req,res){

  console.log(req.body.example1);

  res.render("feedback");
});
  
app.listen(3000, function() {
    console.log("Server is running on port 3000.");
})

您需要向您的服務器發送一個包含表單輸入值的發布請求。 為此,您需要在服務器中添加一個發布路由。

應用程序.js

app.post("/feedback", function(req,res){
    console.log(req.body.example1);
    console.log(req.body.example2);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM