如果所选日期在Js日历中有特定的日期,则将所选日期更改为明天

时间:2018-10-23 22:22:44

标签: jquery jquery-ui-datepicker

如果客户选择关闭日期,我希望日历自动选择第二天。

   var selectedday= $('#calendarid').val();
if(selectedday.getDay() == 6) $(#calendarid).datepicker('setDate', 1);

这使我“ selectedday.getDay()不是函数”,因为我猜想返回的是字符串而不是对象。

有人可以帮忙吗?

亲切的问候,

1 个答案:

答案 0 :(得分:0)

是的,datepicker返回的字符串必须转换为正确的日期。

$('#calendarid').datepicker()

$('#calendarid').change(function() {
  selectedDate = new Date($(this).val())
  if (selectedDate.getDay() == 0) { // Changed this to sunday
    selectedDate.setDate(selectedDate.getDate() + 1)
    $(this).datepicker('setDate', selectedDate);
    $(this).blur();
  }
})
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input type="text" id="calendarid" />

但是,如果您不发出任何警告,这可能会使用户感到困惑。您可以改为禁用周日选择:

$('#calendarid').datepicker({
  beforeShowDay: function(date) {
    return [date.getDay() != 0, ""];
  }
})
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input type="text" id="calendarid" />

参考文献: