设置minDate以防止选择开始日期之前的结束日期 [英] Setting minDate to prevent from choosing enddate before start date

查看:132
本文介绍了设置minDate以防止选择开始日期之前的结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个开始日期和结束日期供我选择日期.我正在使用jQuery datepicker库.

I have a start date and end date for me to choose the date. I am using jQuery datepicker library.

这是我现在的功能:

$(function() {

        $(".txtEndDate").datepicker({
            changeMonth: true,
            changeYear: true,
            showOn: 'button',
            buttonImage: '../../../images/calendar.png',
            buttonImageOnly: true,
            title:'Click to open calendar',
            alt:'Click to open calendar'
        });


    });




    $(function() {

        $(".txtStartDate").datepicker({
            changeMonth: true,
            changeYear: true,
            showOn: 'button',
            buttonImage: '../../../images/calendar.png',
            buttonImageOnly: true,

            title:'Click to open calendar',
            alt:'Click to open calendar',

            onSelect: function() {
                var start = $(this).datepicker("getDate");
                start.setDate(start.getDate() + 7);
                $(".txtEndDate").datepicker("setDate", start);
            }

        });



    });

它可以工作,但是我想在不允许用户选择今天之前的日期时使用它,因为无论如何这都没有意义.例如,选择2018-01-01应该无效.你明白了.

It works but I want to make it when users are not allowed to choose a date before today because it doesn't make sense anyway.For example,choosing 2018-01-01 is not supposed to be valid. You get the idea.

找到了一份建议使用minDate解决此问题的文档,其中说:

Found a documentation that suggests using minDate to solve this.It says:

//initialize the datepicker
$( ".selector" ).datepicker({
  minDate: new Date(2007, 1 - 1, 1)
});

//get or set mindate option
// Getter
var minDate = $( ".selector" ).datepicker( "option", "minDate" );

// Setter
$( ".selector" ).datepicker( "option", "minDate", new Date(2007, 1 - 1, 1) );

由于我对字段代表什么感到困惑,有什么办法可以防止人们选择今天之前的任何日期?

Seeing as I am confused on what the fields represent, is there a way I could prevent people from selecting any dates before today?

我尝试了以下解决方案: jQuery Date Picker-禁用过去的日期

I tried this solution: jQuery Date Picker - disable past dates

但是它并没有禁用它,仍然允许我选择.

But it doesn't disable it and still allows me to choose.

推荐答案

您可以使用datepicker插件的minDate属性禁用过去的日期,如下所示.

You can disable past dates using minDate property of datepicker plugin, as below.

$(".txtEndDate").datepicker({
            minDate: new Date(),  // Add this to diсable past date
            changeMonth: true,
            changeYear: true,
            showOn: 'button',
            buttonImage: '../../../images/calendar.png',
            buttonImageOnly: true,
            title:'Click to open calendar',
            alt:'Click to open calendar'
        });

这篇关于设置minDate以防止选择开始日期之前的结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆