本文介绍了如何根据jquery datepicker中的某个datepicker值禁用将来的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery datepicker,我有4个日期:start_date,end_date,publishing_date和departure_date.Here基于start_date我想要禁用未来的日期到start_date,同样基于end_date我想要从结束日期禁用过去的日期。我无法这样做,尝试了许多选项作为onSelect,但我无法获得所需的禁用。这是我的jquery代码。

I am using jquery datepicker and I have 4 dates:start_date,end_date,publishing_date and departure_date.Here based on start_date I want to disable future dates upto start_date and similarly based on end_date I want to disable past dates from end date. I am not able to do so, tried many option as onSelect but I just can't get the required disabling.Here is my jquery code.

var dateToday = new Date();
var dates = $("#start_date, #end_date").datepicker({
    dateFormat: 'yy-mm-dd',
    minDate: dateToday,
    onSelect: function(selectedDate) {
        var option = this.id == "start_date" ? "minDate" : "maxDate",
        instance = $(this).data("datepicker"),
        date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
        dates.not(this).datepicker("option", option, date);
    }
});

$('#departure_date').datepicker({
    dateFormat: 'yy-mm-dd',
    minDate: $('#end_date').datepicker({
        dateFormat: 'dd-mm-yy'
    }).val()
});

$('#publishing_date').datepicker({
    dateFormat: 'yy-mm-dd',
    maxDate: $('#start_date').datepicker({
        dateFormat: 'dd-mm-yy'
    }).val()
});

欢迎任何建议/帮助。谢谢。

Any suggestions/help is welcome.Thanks in advance.

也尝试使用如下但无济于事。

Also tried using as follows but to no avail.

$('#publishing_date').datepicker({
    dateFormat: 'yy-mm-dd',
    maxDate : ($.datepicker.formatDate('yy-mm-dd', $('#start_date').datepicker("getDate")))
});


推荐答案

根据@Mahesh Sapkal的建议我做了以下更改到我的代码。

Based on suggestions by @Mahesh Sapkal I made following changes to my code.

var dateToday = new Date();
var dates = $("#start_date, #end_date").datepicker({
    dateFormat: 'yy-mm-dd',
    minDate: dateToday,
    onSelect: function(selectedDate) {
          var option = this.id == "start_date" ? "minDate" : "maxDate",
          instance = $(this).data("datepicker"),
          date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
          if(this.id == "start_date"){
                   $("#publishing_date").datepicker({
                         dateFormat: 'yy-mm-dd',
                         minDate: dateToday,
                         maxDate: date
                    });
          }
          if(this.id == 'end_date'){
                   $("#departure_date").datepicker({
                         dateFormat: 'yy-mm-dd',
                         minDate: date
                   })
          }
          dates.not(this).datepicker("option", option, date);
     }

});
不知道为什么,但他的建议是
$(#departnd_date)。datepicker(option,minDate,date);
对我不起作用。

});Don't know why but his suggestion of$( "#departure_date" ).datepicker( "option", "minDate",date );isn't working for me.

这篇关于如何根据jquery datepicker中的某个datepicker值禁用将来的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:59