Uncaught SyntaxError: Invalid left-hand side in assignment in JQuery UI Datepicker

Uncaught SyntaxError: Invalid left-hand side in assignment in JQuery UI Datepicker

我正在使用 JQuery-UI 日期选择器,如下所示:

< script type = "text/javascript" >
  var holidayDays = [];
  var weekendInclusives;
  var holidayInclusives;
$(document).ready(function() {
  $(document).on('change', '#leave_type', function() {
    var air_id = $(this).val();

    var a = $(this).parent();

    var op = "";

    $.ajax({
      type: 'get',
      url: '{{ route('
      get.leavecounts.all ') }}',
      data: {
        'id': air_id
      },
      dataType: 'json', //return data will be json
      // console.log("Its Change !");
      success: function(data) {
        $('#weekendinclusive').val(data.weekendinclusive);
        $('#holidayinclusive').val(data.holidayinclusive);
        holidayDays = data.nationalholidays;
        weekendInclusives = parseInt($("#weekendinclusive").val());
        holidayInclusives = parseInt($("#holidayinclusive").val());
      },
      error: function() {

      }
    });
  });
}); <
/script>

然后,我尝试使用基于函数值的 if 语句:

< script type = "text/javascript" >
  $(document).ready(function() {
     var holidayInclusives = $('#holidayinclusive').val();
     var weekendInclusives = $('#weekendinclusive').val(); 
    $("#leave_days").on('keyup blur', function(e) {
      var periodval = parseInt($("#leave_days").val());
      holidayDays = holidayDays;
      var startDate = $('.commencement_date');
      var endDate = $('.resumption_date');

      var dte = startDate.datepicker("getDate");
      dte.setDate(dte.getDate() + periodval);
      endDate.datepicker("setDate", dte);
    });

    function noWeekendsAndHolidays(date) {
      var ymd = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
      //if u have to disable a list of day
      var removeDays = holidayDays;
      console.log(ymd);
      if ($.inArray(ymd, removeDays) >= 0) {
        return [false];
      } else {
        //Show accept sundays
        var day = date.getDay();
        return [(day == 1 || day == 2 || day == 3 || day == 4 || day == 5)];
      }
    }

    function noHolidays(date) {
      var ymd = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
      //if u have to disable a list of day
      var removeDays = holidayDays;
      console.log(ymd);
      if ($.inArray(ymd, removeDays) >= 0) {
        return [false];
      }
    }

$('.commencement_date').datepicker({
    dateFormat: 'dd-mm-yy',
    changeMonth: true,
    changeYear: true,
    showAnim: 'slideDown',
    duration: 'fast',
    minDate: 1,
    setDate: new Date(),
    yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
    beforeShowDay: function(date) {
        if (weekendInclusives == 0 && holidayInclusives == 0) {
            return noWeekendsAndHolidays(date);
        }
        else if (weekendInclusives == 0 && holidayInclusives == 1) {
            return noHolidays(date);
        }
        else if (weekendInclusives == 1 && holidayInclusives == 0) {
            return $.datepicker.noWeekends(date);
        }
    }
});

    $('.resumption_date').datepicker({
      dateFormat: 'dd-mm-yy',
      changeMonth: true,
      changeYear: true,
      showAnim: 'slideDown',
      duration: 'fast',
      minDate: +1,
      beforeShowDay: $.datepicker.noWeekends,
      yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
      enableOnReadonly: true,
      beforeShow: function(i) {
        if ($(i).attr('readonly')) {
          return false;
        }
      }
      // disabled: true,
      // beforeShowDay: $.datepicker.noWeekends
    });
  }); <
/script>

然后我得到这个错误:

create:990 Uncaught SyntaxError: Invalid left-hand side in assignment

它指向:

  if (weekendInclusives = 0 && holidayInclusives = 0) {
    beforeShowDay: noWeekendsAndHolidays;
  }
  else if (weekendInclusives = 0 && holidayInclusives = 1) {
    beforeShowDay: noHolidays;
  }
  else if (weekendInclusives = 1 && holidayInclusives = 0) {
    beforeShowDay: $.datepicker.noWeekends;
  },

当我将'='更改为'=='时,错误更改为:

Uncaught SyntaxError: Unexpected token '=='

我该如何解决这个问题?

谢谢

您不能在这样的对象声明中使用 if 语句。您需要将对象创建为它自己的变量,然后您可以使用 if 来设置属性。然后你可以将该对象传递给 datepicker().

let dateOpts = {
    dateFormat: 'dd-mm-yy',
    changeMonth: true,
    changeYear: true,
    showAnim: 'slideDown',
    duration: 'fast',
    minDate: 1,
    setDate: new Date(),
    yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
};

if (weekendInclusives == 0 && holidayInclusives == 0) {
    dateOpts.beforeShowDay = noWeekendsAndHolidays;
}
else if (weekendInclusives == 0 && holidayInclusives == 1) {
    dateOpts.beforeShowDay = noHolidays;
}
else if (weekendInclusives == 1 && holidayInclusives == 0) {
    dateOpts.beforeShowDay = $.datepicker.noWeekends;
}

$('.commencement_date').datepicker(dateOpts);

更新:如果您想在 weekendInclusivesholidayInclusives 变量更新时更新日期选择器,那么您需要 beforeShowDay 检查这个。

$('.commencement_date').datepicker({
    dateFormat: 'dd-mm-yy',
    changeMonth: true,
    changeYear: true,
    showAnim: 'slideDown',
    duration: 'fast',
    minDate: 1,
    setDate: new Date(),
    yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
    beforeShowDay: function(date) {
        if (weekendInclusives == 0 && holidayInclusives == 0) {
            return noWeekendsAndHolidays(date);
        }
        else if (weekendInclusives == 0 && holidayInclusives == 1) {
            return noHolidays(date);
        }
        else if (weekendInclusives == 1 && holidayInclusives == 0) {
            return $.datepicker.noWeekends(date);
        }
    }
});