简体   繁体   English

根据javascript或jquery中的选定日期禁用日期选择器的先前日期

[英]Disable previous dates of a datepicker according to selected date in javascript or jquery

I have two datepickers like startdate and enddate.If i select 18th june 2015 in startdate i have to disable previous dates in datepicker to 18thjune2015 in enddate.我有两个日期选择器,如 startdate 和 enddate。如果我在 startdate 中选择 2015 年 6 月 18 日,我必须将 datepicker 中的先前日期禁用到 enddate 中的 18thjune2015。

Here is my code.这是我的代码。

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

For end-date:对于结束日期:

    var startdate = $("#txtstartdate").val();
    $("#txtenddate").datepicker({ minDate: startdate }); 

Its not working.它不工作。 Can anyone pls help me on this.任何人都可以帮我解决这个问题。 thank you.谢谢你。

You need to set it using the option method on change of start date您需要使用更改开始日期的选项方法进行设置

 $("#txtstartdate").datepicker({ minDate: 0, onSelect: function(date) { $("#txtenddate").datepicker('option', 'minDate', date); } }); $("#txtenddate").datepicker({});
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/redmond/jquery-ui.css"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script> <input id="txtstartdate" /> <input id="txtenddate" />

If you use input tag with attribute type="datetime-local" try this:如果您使用属性type="datetime-local"输入标记,请尝试以下操作:

let today = new Date().toLocaleString('sv-SE').slice(0, 16).replace(' ', 'T');
document.getElementById('someId').setAttribute("min", today);

Try adding yearRange :尝试添加yearRange

$("#txtenddate").datepicker({ 
    minDate: startdate,
    yearRange: '1999:2020', 
}); 

You have to use option你必须使用option

insted of插入的

$("#txtenddate").datepicker({ minDate: startdate }); 

Do This:做这个:

$("#txtenddate").datepicker('option', 'minDate', date);

Change mindate in datepicker 在日期选择器中改变主意

$("#txtstartdate").datepicker({
  minDate: 0,
  onSelect: function(date) {
    $("#txtenddate").datepicker('option', 'minDate', date);
  }
});

$("#txtenddate").datepicker({});



<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/redmond/jquery-ui.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>

<input id="txtstartdate" />
<input id="txtenddate" />

If minDate is not working, you can try startDate it will work definitely.如果minDate不起作用,您可以尝试startDate它肯定会起作用。

$('#datepicker4').datepicker({
  autoclose: true,
  startDate: new Date() 
 });

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

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