第二个星期天是空的

时间:2017-03-14 09:53:30

标签: javascript jquery html5

脚本有问题,如果你运行代码片段并选择星期日,你会发现每个星期天都是空的我不知道为什么你能帮助谢谢

$(function() {
  var weekStart = new Date(2017, 1, 27);
  var roster = ['work', 'off', 'off', 'work', 'work', 'work', 'work', 'off', 'work', 'work', 'work', 'off', 'off', 'work'];

  $('#date').datepicker({
    minDate: weekStart,
    onSelect: function(dateStr) {
      var date = $(this).datepicker('getDate');
      var days = Math.floor((date.getTime() - weekStart.getTime()) / (24 * 60 * 60 * 1000));
      var week = Math.floor(days / 7) % 2;
      $('#week').val(week + 1);
      $('#day').val($.datepicker.formatDate('DD', date));
      $('#work').val(roster[week * 7 + date.getDay() - 1]);
    }
  });
});
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="/scripts/jquery.ui.datepicker-it.js"></script>

<p>Date: <input type="text" id="date"></p>
<p>Day: <input type="text" id="day"></p>
<p>week: <input type="text" id="week"></p>
<p>work: <input type="text" id="work"></p>

2 个答案:

答案 0 :(得分:1)

getDay()返回星期日0,你的周数为0或1,所以

week * 7 + date.getDay() - 1

返回-1作为结果,它不会从名册表中获取值。

所以使用

$('#work').val(roster[(week) * 7 + date.getDay()]);

答案 1 :(得分:0)

每周加1?

$('#work').val(roster[(week+1) * 7 + date.getDay() - 1]);