【发布时间】:2021-03-22 17:51:06
【问题描述】:
我有一个表格,其中包括一周中的几天以及每天、工作日和周末选项。每行有 2 个select 项目来选择更新的开始时间和结束时间。在每一行的末尾都有一个切换按钮,用于启用和禁用该特定日期。用户可以单独选择每一天,也可以选择每天选项。当他们启用切换时,禁用的选择元素将启用,以便他们可以选择特定日期的时间。
如上图,用户可以同时选择 Weekdays 和 Weekend,这应该等于 Everyday 吧?所以我想做的是,当他们同时启用 Weekdays 和 Weekend 时,应该启用 Everyday 切换,而其他两个再次禁用。
这是我的数据和我的方法;
data() {
return {
everydayCheck: false,
weekdaysCheck: false,
weekendCheck: false,
customCheck: false,
mondayCheck: false,
tuesdayCheck: false,
wednesdayCheck: false,
thursdayCheck: false,
fridayCheck: false,
saturdayCheck: false,
sundayCheck: false,
}
},
methods: {
isDisabled: function(){
if(this.everydayCheck){ return !this.everydayCheck; }
else if(this.weekdaysCheck){ return !this.weekdaysCheck; }
else if(this.weekendCheck){ return !this.weekendCheck; }
else if(this.customCheck){ return !this.customCheck; }
else if(this.mondayCheck){ return !this.mondayCheck; }
else if(this.tuesdayCheck){ return !this.tuesdayCheck; }
else if(this.wednesdayCheck){ return !this.wednesdayCheck; }
else if(this.thursdayCheck){ return !this.thursdayCheck; }
else if(this.fridayCheck){ return !this.fridayCheck; }
else if(this.saturdayCheck){ return !this.saturdayCheck; }
else if(this.weekdaysCheck && this.weekendCheck){
return this.everydayCheck, !this.weekdaysCheck, !this.weekendCheck;
}
else { return !this.sundayCheck; }
},
},
else 之前的最后一个else if 说如果weekdayCheck 和weekdaysCheck 为真,则将everydayCheck 设置为true 并将其他设置为假,还是我错了?它不起作用。我错过了什么?
【问题讨论】:
-
JavaScript 不支持返回多个值的函数。所以
else之前的最后一个else if是无效的 -
你如何使用
isDisabled()呢?我可以说它只返回一个布尔值。 -
我是这样使用的。单击相关复选框时,该功能就像一个切换开关。它在真假之间切换数据。无论如何,我可以在检查两个复选框时控制吗?当他们两个被选中时,我需要去启用另一个,然后禁用这两个。
-
@Yashin Demirkaya 我在那里看到了你的问题。所以这个函数
isDisabled只在它们在工作日切换时运行,而你希望这个函数也运行在everyday和weekend上,不是吗? -
我为每个表格元素(每天、工作日、周末和其他 7 个元素)设置了 isDisabled 函数。无论选择哪一个,该函数都会对其进行检查并切换它。所以可以同时选择Weekend和Weekdays,如果是,我需要触发函数去选择days。
标签: css vue.js toggle show-hide