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

标签 javascript vue.js vuejs2

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

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

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/

相关文章:

javascript - 如何在 IE 中自动允许被阻止的内容?

vue.js - 如何从具有相同 v-model 名称的多个单选按钮获取单选按钮值?

css - SASS 变量未在 VueJS 应用程序中呈现正确的结果

javascript - 如何在 Javascript 中创建带有图像的旋转轮?

javascript - 使用 chartjs 在圆环图中标记问题

javascript - 使用 Array.sort() 获得一个比较函数来对 ASC 和 DESC 进行排序?

javascript - Vue forEach 计算

vue.js - 无法获取数组的索引

vue.js - 尝试在同一页面上显示未经身份验证和经过身份验证的内容时闪烁的内容

javascript - Vue.js <transition-group> 仅在最后一个元素上应用动画