繁体   English   中英

如何在日历javascript中禁用以前的日期?

[英]How to disable previous dates in calendar javascript?

我正在尝试禁用日历中的先前日期。 我正在使用此代码

我的HTML代码如下。

<input type="text" required name="date_from" name="date_from" class="mydate input-text full-width" placeholder="Departure Date" />

我的脚本代码。

<script type="text/javascript">
 $(".mydate").datepicker({
       format:'yyyy-mm-dd',
       autoclose: true
   });
    $(".flexslider").flexslider({
        animation: "fade",
        controlNav: false,
        animationLoop: true,
        directionNav: false,
        slideshow: true,
        slideshowSpeed: 5000
    });
</script>

它显示没有禁用以前日期的日历。

试试这个:

$( ".mydate" ).datepicker({ minDate: 0});

使用bootstrap date picker 这些是要包含的文件

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css" rel="stylesheet" type="text/css" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>

使用此代码禁用以前的日期

HTML 输入字段

<input id="date" data-provide="datepicker" name="date_from" >

爪哇脚本

var date = new Date();
date.setDate(date.getDate());

$('#date').datepicker({ 
startDate: date
});

在日期选择器的初始化中设置 minDate

minDate:new Date()


$(".mydate").datepicker({
       format:'yyyy-mm-dd',
       autoclose: true,
       minDate:new Date()
   });

您必须设置 minDate 选项。 本论坛上的示例+小提琴:

jQuery 日期选择器 - 禁用过去的日期

您需要在应用日期选择器时设置 minDate 选项。

试试这个,

<script type="text/javascript">
$(".mydate").datepicker({
       format:'yyyy-mm-dd',
       autoclose: true,
       minDate: 0,   
}); 
</script>

从您的格式来看,您可能正在使用引导程序日期选择器。 所以使用startDate禁用以前的日期

$(".mydate").datepicker({
       format:'yyyy-mm-dd',
       startDate: new Date(),
       autoclose: true
   });

试试这个代码...如果用户提交的日期小于今天的日期然后显示警报消息以更改日期,这将限制用户。

HTML代码

    <form name="myform" onsubmit="return validateDateOfAppointment()">
    <input type="date" name="Date of Appointment" placeholder="Date of Appointment" id="Date" />
</form>

JavaScript 代码

function validateDateOfAppointment(){
        var date=document.getElementById("Date").value;
        var d=new Date();
        var x=d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
        var checkDate=date.substr(8,2);
        var equalDate=d.getDate();
        var checkMonth=date.substr(5,2);
        var equalMonth=d.getMonth();
        var checkYear=date.substr(0,4);
        var equalYear=d.getFullYear();
        if(checkMonth>=equalMonth){
            if(checkDate<equalDate){
            alert("Date cannot be less than today!! ");
            return false;
            }
        }
        else if(checkMonth<equalMonth){
            if(checkYear<equalYear){
                alert("Date cannot be less than today!! ");
                return false;
            }
        }
     }

暂无
暂无

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

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