[英]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 選項。 本論壇上的示例+小提琴:
您需要在應用日期選擇器時設置 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.