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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31204306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:23:11  来源:igfitidea点击:

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

javascriptjqueryjquery-uidatepicker

提问by Priya

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.

它不工作。任何人都可以帮我解决这个问题。谢谢你。

回答by Arun P Johny

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" />

回答by Bhojendra Rauniyar

Try adding yearRange:

尝试添加yearRange

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

回答by Somnath Kharat

You have to use option

你必须使用 option

insted of

插入的

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

Do This:

做这个:

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

Change mindate in datepicker

在日期选择器中改变主意

回答by RANJITH

$("#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" />