我想根据复选框状态更新选择布尔值true或false。 单击复选框如何更新它。我做了一个表达方式,我不知道如何从复选框中获取数据。此代码使100个Ajax请求100个复选框成为可能。
模式:
var dataModel = new Schema({
name:{type:String,required:true}
,
email:{type:String,required:true},
department:{type:String,required:true},
section:{type:String,required:true},
data1:{ type:String, required:true}
,
data2:{ type:String, required:true},
data:{ type:String, required:true},
selection:Boolean
});
<% foundStudents.forEach(function(data,i){%>
<tr>
<td>
<%=i+1%>
</td>
<td>
<%= data.name %>
</td>
<td>
<%= data.department %>
</td>
<td>
<%= data.section %>
</td>
<td class="text-primary">
<button type="button" class="btn btn-outline-danger">View</button>
</td>
<td>
<input id="radio" class="checkbox" type="checkbox" name="selection" value="<%=data._id%>" <%= data.selection==true ? "checked" : "" %>>
</td>
</tr>
<%})%>
$(".btn").on('click', function () {
var checkbox_value = "";
$(":checkbox").each(function (i,element) {
var ischecked = $(this).is(":checked");
if (ischecked) {
$.ajax({
url: '/admin/select/'+$(this).val(),
type: 'PUT',
data:{boolVal:true},
success: function(response) {
console.log(data);
console.log(url);
}
});
}
else{
$.ajax({
url: '/admin/select/'+$(this).val(),
type: 'PUT',
data:{boolVal:false},
success: function(response) {
console.log(data);
console.log(url);
}
});
节点代码
router.put("/admin/select/:id",function(req,res){
var id = req.params.id;
dataModel.findByIdAndUpdate(id,{ $set: { selection: req.body.boolVal } },function(err,selectedStudent){
if(err)
{
console.log(err);
console.log(req.body.boolVal)
}
else{
console.log("put request recieved");
console.log(req.body.boolVal)
}
})
});
router.get("/shortlistedTable",function(req,res){
dataModel.find({selection:"true"},function(err,shortlistedStudents){
res.render("./admin/shortlistedTable",{shortlistedStudents:shortlistedStudents})
});
});
如何在mongoDB上更新布尔值?请帮忙吗?