本文介绍了jQuery Date Picker - 禁用以前的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在from / to字段中,人们不应该能够查看或选择前一个日期到目前为止。



这是我的代码:

  $ (function(){
var dates = $(#from,#to).datepicker({
defaultDate:+ 1w,
changeMonth:true,
numberOfMonths :1,
onSelect:function(selectedDate){
var option = this.id ==from?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);
}
});
});

有人可以告诉我如何禁用迄今为止的日期。

解决方案

您必须创建一个新的日期对象,并将其设置为 minDate ,当您初始化datepickers

 < label for =from> From< / label> < input type =textid =fromname =from/> < label for =to> to< / label> < input type =textid =toname =to/> 

var dateToday = new Date();
var dates = $(#from,#to)。datepicker({
defaultDate:+ 1w,
changeMonth:true,
numberOfMonths:3,
minDate:dateToday,
onSelect:function(selectedDate){
var option = this.id ==from?minDate:maxDate,
instance = $ ).data(datepicker),
date = $ .datepicker.parseDate(instance.settings.dateFormat || $ .datepicker._defaults.dateFormat,selectedDate,instance.settings);
dates.not (this).datepicker(option,option,date);
}
});

编辑 - 从您的评论,现在它的工作原理


I am trying to have a date Range select using the UI date picker.

in the from/to field people should not be able to view or select dates previous to the present day.

This is my code:

$(function() {
    var dates = $( "#from, #to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function( selectedDate ) {
            var option = this.id == "from" ? "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 );
        }
    });
});

Can some one tell me how to disable dates previous the to the present date.

解决方案

You must create a new date object and set it as minDate when you initialize the datepickers

<label for="from">From</label> <input type="text" id="from" name="from"/> <label for="to">to</label> <input type="text" id="to" name="to"/>

var dateToday = new Date();
var dates = $("#from, #to").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 3,
    minDate: dateToday,
    onSelect: function(selectedDate) {
        var option = this.id == "from" ? "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);
    }
});

Edit - from your comment now it works as expected http://jsfiddle.net/nicolapeluchetti/dAyzq/1/

这篇关于jQuery Date Picker - 禁用以前的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 02:35