gpt4 book ai didi

javascript - 如何从计算值循环每月的每一天

转载 作者:行者123 更新时间:2023-11-30 14:40:44 24 4
gpt4 key购买 nike

我正在创建一个注册表单,用户必须在其中选择出生日期。

我循环遍历月份列表并相应地输出日期。

let selectedMonth = 'April'

new Vue ({
el: '#app',
data: {
months: [
{month: 'January', days: 31},
{month: 'February', days: 28},
{month: 'March', days: 31},
{month: 'April', days: 30},
{month: 'May', days: 31},
{month: 'June', days: 30},
{month: 'July', days: 31},
{month: 'August', days: 31},
{month: 'September', days: 30},
{month: 'October', days: 31},
{month: 'November', days: 30},
{month: 'December', days: 31},
]
},
computed: {
filterDays() {
return this.months.filter(function(value) {
return value.month === selectedMonth;
});
}
},
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>  

<div id = "app">
<select id="dob">
<option v-for="day in filterDays" :value="day.days">{{ day.days }}</option>
</select>
</div>

上面的程序只输出<option value = "30">30</option>而不是循环 day in 30 .

最佳答案

filterDays 返回一个月对象集合。如果你想循环一个数字,使用它作为循环源:filterDays[0].days

<option v-for="day in filterDays[0].days" :value="day">{{ day }}</option>

或者通过返回那里的天数来修复计算函数。

computed: {
filterDays() {
// Select the first item here.
const month = this.months.filter(value => value.month === selectedMonth)[0];

// If the month was found, return its days, otherwise, undefined.
return month && month.days;
}
},

关于javascript - 如何从计算值循环每月的每一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49709745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com