作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这些始终保持选中状态的复选框,但我只想在与它们关联的对象的名为 include 的属性设置为 true 时检查它们。
<小时/>我没有在模板上设置选中的属性,所以我不知道为什么默认情况下会选中它们?希望有人能指出我的错误。
我缺席的学生数据:
absentStudents: [{
"id": 207,
"first_name": "Gabriel De Manuel",
"include": false,
"avatar": null,
"group_id": 24,
"full_name": ", Gabriel De Manuel",
"exclude": false ,
"isGrouped": false,
}, {
"id": 208,
"first_name": "Francisco",
"include": false,
"avatar": null,
"group_id": 24,
"full_name": ", Francisco",
"exclude": false,
"isGrouped": false,
}, {
"id": 209,
"first_name": "Rosa",
"include": false,
"avatar": null,
"group_id": 24,
"full_name": ", Rosa",
"exclude": false,
"isGrouped": false,
}],
excludeAbsent: false,
//my v-model is created by the following function
created() {
this.absentStudentsSelected = this.absentStudents.map(x => x.id);
},
我已经为我的列表呈现了这些复选框
<ul>
<li v-for="absentStudent in absentStudents" class="list-unstyled">
<input type="checkbox" @click="check($event)" v-model="absentStudentsSelected" :value="absentStudent.id">
{{ absentStudent.first_name }}
</li>
</ul>
只要 this.excludeAbsent 为 true,请选中每个学生的复选框
check: function(e){
if(this.excludeAbsent === true){ //if we want to include absent students
for(var i = 0; i< this.absentStudents.length; i++){
if(e.target.value == this.absentStudents[i].id){
this.absentStudents[i].include = true
}else{
this.absentStudents[i].include = false
}
}
}
最佳答案
absentStudentsSelected
应在数据选项中初始化为空数组:
let app = new Vue({
el: "#app",
data() {
return {
absentStudents: [{
"id": 207,
"first_name": "Gabriel De Manuel",
"include": false,
"avatar": null,
"group_id": 24,
"full_name": ", Gabriel De Manuel",
"exclude": false,
"isGrouped": false,
}, {
"id": 208,
"first_name": "Francisco",
"include": false,
"avatar": null,
"group_id": 24,
"full_name": ", Francisco",
"exclude": false,
"isGrouped": false,
}, {
"id": 209,
"first_name": "Rosa",
"include": false,
"avatar": null,
"group_id": 24,
"full_name": ", Rosa",
"exclude": false,
"isGrouped": false,
}],
excludeAbsent: false,
absentStudentsSelected: []
}
},
methods: {
check: function(e) {
if (this.excludeAbsent === true) {
for (var i = 0; i < this.absentStudents.length; i++) {
if (e.target.value == this.absentStudents[i].id) {
this.absentStudents[i].include = true
} else {
this.absentStudents[i].include = false
}
}
}
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<ul>
<li v-for="absentStudent in absentStudents" class="list-unstyled">
<input type="checkbox" @click="check($event)" v-model="absentStudentsSelected" :value="absentStudent.id"> {{ absentStudent.first_name }}
</li>
</ul>
</div>
关于javascript - 仅在条件vue上复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61179264/
我是一名优秀的程序员,十分优秀!