首页 > 解决方案 > 如何使用复选框输入从 false 更改为 true?

问题描述

如果有人可以帮助我。我想做下一件事。使用复选框我想选择要放在索引页面上的帖子

这是我的架构

var foodSchema = new mongoose.Schema({
       title:String,
       image:String,
       text:String,
       video:String,
       checkBox:{type:Boolean, default:false},
       created:{type: Date, default:Date.now},
       comments:[{

                   type: mongoose.Schema.Types.ObjectId,
                   ref:"comment"
              }
    ]
});

这是我的 new.ejs 文件

     <spam class="hederSpam">
          <input type="checkbox" name="checked" value="true">
           post
    </spam>
```

here i wanna to display


``` <%newFood.forEach(function(check){ %>
      <%if(check.checkBox===true){%>
          <h3 class="indexh2"><%= check.title %></h3>
              <%=  check.checkBox%>
         <a href="/food/<%= check._id%> " >Read More</a>
  <% })%>

在 site.js 文件中我试图这样做但每次都给我错误..

   food.create(req.body.foodForm, function(err, postFood){
       var n = req.body.checked;
       if(err){
           console.log("err")
       }else{

           if( n == "true"){

               food.checkBox=true;
           }

              res.redirect("/food");


       }
   }) 
});

标签: node.jsexpressmongoose

解决方案


这似乎是一段奇怪的代码:

console.log("err)

我在这里放置下划线的地方也应该有空格:

 name="checked"_value="true"

推荐阅读