简体   繁体   English

如何使两个日期选择器在html中相互依赖?

[英]How to make a two date picker depend on each other in html?

Suppose there are two date pickers. 假设有两个日期选择器。 The second date picker will be fully depended on the first one as given in the snippet 第二个日期选择器将完全取决于代码段中给出的第一个

 $( function() { $("#txtFromDate").datepicker({ numberOfMonths: 2, minDate:0, onSelect: function(selected) { $("#txtToDate").datepicker("option","minDate", selected) } }); $("#txtToDate").datepicker({ numberOfMonths: 2, onSelect: function(selected) { $("#txtFromDate").datepicker("option","maxDate", selected) } }); }); 
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> From: <input type="text" id="txtFromDate" /> To: <input type="text" id="txtToDate" /> 

If we select a date from the first date picker is there any method if we leave empty the second date picker then the date of second date picker will set to the same date as the first but after the one year. 如果我们从第一个日期选择器中选择一个日期,那么如果将第二个日期选择器留空,则有任何方法可以将第二个日期选择器的日期设置为与第一个日期选择器相同的日期,但在一年之后。 It means if we set the date in first is 07-29-2018 then if we left empty the second date picker then the date will automatically taken as 07-29-2019 . 这意味着如果我们将第一个日期设置为07-29-2018日,那么如果我们将第二个日期选择器留空,则日期将自动作为07-29-2019 Is there any method for doing this. 有什么方法可以做到这一点。 can any body please help me. 有没有人可以帮助我。 Thank you. 谢谢。

You set default date for second datepicker on select of first datepicker and vice versa. 您可以在选择第一个日期选择器时设置第二个日期选择器的默认日期,反之亦然。 Here is an example. 这是一个例子。

 $( function() { $("#txtFromDate").datepicker({ numberOfMonths: 2, minDate:0, onSelect: function(selected) { $("#txtToDate").datepicker("option","minDate", selected); $("#txtToDate").datepicker("setDate", '+1y'); } }); $("#txtToDate").datepicker({ numberOfMonths: 2, onSelect: function(selected) { $("#txtFromDate").datepicker("option","maxDate", selected) } }); }); 
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> From: <input type="text" id="txtFromDate" /> To: <input type="text" id="txtToDate" /> 

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

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