Javascript 如何通过post方法发送开关切换表单

Javascript 如何通过post方法发送开关切换表单,javascript,mongodb,express,ejs,Javascript,Mongodb,Express,Ejs,我正在使用一个ejs模板,并通过method='post'与app.js通信。我如何发送表格 对于输入文本,以下方法有效 <form action="/login" method="POST"> <div class="form-group"> <label for="email">Email</label> <input type="email" class="

我正在使用一个ejs模板,并通过method='post'与app.js通信。我如何发送表格

对于输入文本,以下方法有效

<form action="/login" method="POST">
            <div class="form-group">
              <label for="email">Email</label>
              <input type="email" class="form-control" name="username">
            </div>
            <div class="form-group">
              <label for="password">Password</label>
              <input type="password" class="form-control" name="password">
            </div>
            <button type="submit" class="btn btn-dark">Login</button>
</form>
它应该记录切换的项目,但没有返回任何内容

app.js(服务器端)的几乎全部代码


我无法理解您的服务器代码。请发送您的完整代码。这是主窗体的完整代码。对于main.ejs和app.js的app.post(“/main”)。常量app=express();const bodyParser=require(“body parser”);use(bodyParser.urlencoded({extended:true}));app.get(“/main”,(req,res)=>{res.render(“main”);});app.post(“/main”,(req,res)=>{console.log(req.body.toggle1,req.body.toggle2);});难道你没有忘记提交按钮吗?`是的,我正在寻找一种不用按钮的提交方式。如果选中复选框而不单击按钮,我不能自动提交表单吗?
 <form action="/main" method="POST">
    <div class="custom-control custom-switch">
      <input type="checkbox" class="custom-control-input" id="sw1" name="toggle2">
      <label class="custom-control-label" for="sw1">On/Off</label>
    </div>
    <div class="custom-control custom-switch">
      <input type="checkbox" class="custom-control-input" id="sw2" name="toggle1">
      <label class="custom-control-label" for="sw2">on/off</label>
    </div>
    </form>
app.post("/main",(req,res)=>{
  console.log(req.body.toggle1,req.body.toggle2);
});
 const app = express();
    const bodyParser = require("body-parser");
    app.use(bodyParser.urlencoded({extended: true}));
    app.get("/main",(req,res)=>{
      res.render("main");
    });
    app.post("/main",(req,res)=>{
      console.log(req.body.toggle1,req.body.toggle2);
    });