如何使用复选框输入将false更改为true?

时间:2019-04-12 02:03:16

标签: node.js express mongoose

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

这是我的模式

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");


       }
   }) 
});

1 个答案:

答案 0 :(得分:0)

这看起来很奇怪:

console.log("err)

另外,我应该在此处加下划线:

 name="checked"_value="true"