简体   繁体   English

jQueryUI DatePicker-将单击事件添加到minDate和maxDate之外的日期

[英]jQueryUI DatePicker - Add click events to dates outside of minDate and maxDate

I am using 2 jQueryUI DatePickers to select a start and end date. 我正在使用2 jQueryUI DatePickers选择开始和结束日期。 My requirements are that I must specify a start and end date separately, rather than selecting a date range on a single calendar. 我的要求是我必须分别指定开始日期和结束日期,而不是在单个日历上选择日期范围。

When I select the start date, I set the minDate of the end date to be equal to it. 当选择开始日期时,将结束日期的minDate设置为与其相等。 Vice versa with the end date. 反之亦然,结束日期。

$('.datepicker').datepicker({
   beforeShowDay: $.datepicker.noWeekends,
   dateFormat: 'DD dd-mm-yy',
   onSelect: function () {
      SetMinMaxValuesOfDatePickers();
   }
});

static SetMinMaxValuesOfDatePickers() {
   let newMinDate = $('#start-date').datepicker('getDate');
   let newMaxDate = $('#end-date').datepicker('getDate');

   if (newMinDate != null) {
      $('#end-date').datepicker('change', { minDate: newMinDate });
   }
   if (newMaxDate != null) {
      $('#start-date').datepicker('change', { maxDate: newMaxDate });
   }
}

The disabling of the dates before the minDate and maxDate gives me a partial range selector that suits my needs better than an actual range selector. 禁用minDatemaxDate之前的日期会给我一个部分范围选择器,它比实际范围选择器更适合我的需求。

在此处输入图片说明

However, I want the user to be able to click dates before the minDate and for it to reset the min / maxDate on both DatePickers. 但是,我希望用户能够单击minDate 之前的日期并为其重置两个DatePickers上的min / maxDate I tried to achieve this by adding a click event to the disabled cells in the calendar: 我试图通过向日历中的禁用单元格添加click事件来实现此目的:

$('.ui-state-disabled').on('click', function () {
   alert("This doesn't appear!");
   $("#start-date").datepicker("option", "minDate", null);
   $("#end-date").datepicker("option", "minDate", null);
});

Unfortunately, these click events never trigger. 不幸的是,这些点击事件永远不会触发。 I couldn't even select the disabled <td> s in inspect element . 我什至无法在inspect元素中选择禁用的<td> I investigated and found the CSS property pointer-events: none; 我调查发现CSS属性的pointer-events: none; was being applied to each of the disabled <td> s. 被应用于每个禁用的<td> Removing that allowed me to select disabled cells in the debugger, but I could never get the click events to fire. 删除该代码后,我可以在调试器中选择禁用的单元格,但无法触发点击事件。

Can someone assist me with this? 有人可以协助我吗? If I can get the events to fire when a user clicks a disabled date I can do it. 如果我可以在用户单击禁用日期时触发事件,则可以执行此操作。

Your idea of adding a trigger on the disabled item should work. 您在禁用的项目上添加触发器的想法应该可行。 My guess is you're defining it before the datepicker. 我的猜测是您要在日期选择器之前定义它。

$('.ui-datepicker-unselectable.ui-state-disabled').click(function() {
    alert('invalid date selected');
});

Please see this example: http://jsfiddle.net/Zrz9t/6694/ 请参见以下示例: http : //jsfiddle.net/Zrz9t/6694/

In the end I decided against using the min/max date as a form of range picker. 最后,我决定不使用最小/最大日期作为范围选择器。 It was too hacky and messy. 太杂乱无章了。 I ended up forgoing the range picker aspect of this little widget and just went with plain start date and end date. 我最终放弃了这个小部件的范围选择器方面,只使用了简单的开始日期和结束日期。

@user1477388 - that code is very similar to what I tried and it still did not detect a click event. @ user1477388-该代码与我尝试的代码非常相似,但仍未检测到点击事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM