依靠这一年

时间:2015-12-07 09:43:37

标签: javascript count monthcalendar

我有一个问题,我已经问过Count on the month并感谢上帝,有人帮助了我,但现在我有另一个问题。日期和月份都是自动计算的,但是当它在年底时它不起作用。有谁知道我怎么能让这个工作?

我在上周的最后一个星期一自动设定了日期: 这是现在的样子

enter image description here

当我将日期更改为今年年底时,它会停止计算:

enter image description here

$(document).ready(function() {

            $("#day").change(function () {
                var sDay = parseInt($(this).find('option:selected').val());
                var sMonth = parseInt($(this).next().find('option:selected').val());

                for(var i=0; i < 7; i++)
                {
                    var cDay = 'start_day' + i;
                    var cMonth = 'start_month' + i;
                    if(sDay <= 31){
                        $("[name=" + cDay).val(sDay);
                        $("[name=" + cMonth).val(sMonth);
                    }
                    else {
                        sDay = 1;
                        sMonth++;
                        $("[name=" + cDay).val(sDay);
                        $("[name=" + cMonth).val(sMonth);
                    }
                    sDay++;
                }
            });

function getMonday(d) 
            {
                var day = d.getDay();
                diff = d.getDate() - day + -6; 
                return new Date(d.setDate(diff));
            }

            var day = getMonday(new Date());
            var month = day.getMonth()+1;

            for(var i = 0; i < 7; i++)
            {
                var thisDate = new Date(day.getTime() + (24 * 60 * 60 * 1000 * i)); 
                $('[name="start_day'+i+'"').val(thisDate.getDate());
                $('[name="start_month'+i+'"').val(thisDate.getMonth() + 1);
 }

2 个答案:

答案 0 :(得分:0)

这样的东西?

var sYear = parseInt($(this).next().next().find('option:selected').val());
for(var i=0; i < 7; i++)
   {
      var date = new Date(sYear, sMonth-1, sDay);
      var cDay = 'start_day' + i;
      var cMonth = 'start_month' + i;
      var cYear = 'start_year' + i;
      $("[name=" + cDay).val(date.getDate());
      $("[name=" + cMonth).val(date.getMonth()+1);
      $("[name=" + cYear).val(date.getFullYear());
      sDay++;
   }

答案 1 :(得分:0)

Alexander,这是我使用的代码..

        $(document).ready(function() {

        $("#day").change(function () {
            var sDay = parseInt($(this).find('option:selected').val());
            var sMonth = parseInt($(this).next().find('option:selected').val());
            var sYear = parseInt($(this).next().next().find('option:selected').val());
            for(var i=0; i < 7; i++)
            {
              var date = new Date(sYear, sMonth-1, sDay);
              var cDay = 'start_day' + i;
              var cMonth = 'start_month' + i;
              var cYear = 'start_year' + i;
              $("[name=" + cDay).val(date.getDate());
              $("[name=" + cMonth).val(date.getMonth()+1);
              $("[name=" + cYear).val(date.getFullYear());
              sDay++;
            }
        });
                $("#day").change();
        });

            function getMonday(d) 
            {
                var day = d.getDay();
                diff = d.getDate() - day + -6; 
                return new Date(d.setDate(diff));
            }

            var day = getMonday(new Date());
            var month = day.getMonth()+1;

            for(var i = 0; i < 7; i++)
            {
                var thisDate = new Date(day.getTime() + (24 * 60 * 60 * 1000 * i)); 
                $('[name="start_day'+i+'"').val(thisDate.getDate());
                $('[name="start_month'+i+'"').val(thisDate.getMonth() + 1);
            }