【问题标题】:Date Validation based on current date基于当前日期的日期验证
【发布时间】:2015-11-05 11:14:36
【问题描述】:

我有两个日期,即 Date from 和 Date to,我的问题是,我希望 Date from 只能单击/选择过去的日期直到当前日期,然后 Date to 可以自动选择当前日期

这是代码

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Select a Date Range</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
 $(function() {
    $( "#from" ).datepicker({
      dateFormat: 'yy-mm-dd',
      defaultDate: "+1 	w",
      changeMonth: true,
	  changeYear: true,
      numberOfMonths: 1,
      yearRange: '2006:'+(new Date).getFullYear() ,
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to" ).datepicker({
      dateFormat: 'yy-mm-dd',
      defaultDate: "+1w",
      changeMonth: true,
	  changeYear: true,
      numberOfMonths: 1,
      yearRange: '2006:'+(new Date).getFullYear() ,
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });
  
  </script>
</head>
<body>
 
<label for="from">From</label>
<input type="text" id="from" name="from">
<label for="to">to</label>
<input type="text" id="to" name="to">
 
 
</body>
</html>

【问题讨论】:

  • 可能重复Post.
  • 我试过了,还是不行:(
  • 而且也不一样

标签: javascript jquery date


【解决方案1】:

使用 maxDate 属性我们可以满足您的要求。

请从 Datepicker 中将 maxDate 设置为今天的日期。

$(function() {
   $( "#from" ).datepicker({
    dateFormat: 'yy-mm-dd',
    defaultDate: "+1    w",
    maxDate: new Date(),
    changeMonth: true,
    changeYear: true,
    numberOfMonths: 1,
    yearRange: '2006:'+(new Date).getFullYear() ,
    onClose: function( selectedDate ) {
    $( "#to" ).datepicker( "option", "minDate", selectedDate );
  }
});
  $( "#to" ).datepicker({
    dateFormat: 'yy-mm-dd',
    defaultDate: "+1w",
    minDate : new Date(),
    maxDate: new Date(), 
    changeMonth: true,
    changeYear: true,
    numberOfMonths: 1,
    yearRange: '2006:'+(new Date).getFullYear() ,
    onClose: function( selectedDate ) {
    $( "#from" ).datepicker( "option", "maxDate", selectedDate );
  }
}).datepicker("setDate",new Date());

});

希望这会对你有所帮助。

Fiddle

【讨论】:

  • 谢谢!,Date From 运行良好,但我希望 Date To 自动选择当前日期。
  • @Vista 试试这个小提琴链接jsfiddle.net/1421knt5/3 现在我将使用 setDate 方法为日期选择器设置日期。
  • 有没有可能,Date To 不能只选择当前日期?对不起:(
  • @vista 试试这个小提琴链接jsfiddle.net/1421knt5/4。现在我将 minDate 和 maxDate 都设置为日期选择器的今天日期。我更新了,请重新检查。
  • 谢谢!这就是我需要的:D
【解决方案2】:

$(function() {
    $( "#from" ).datepicker({
      dateFormat: 'yy-mm-dd',
      defaultDate: "+1 	w",
      changeMonth: true,
	  changeYear: true,
      numberOfMonths: 1,
      yearRange: '2006:'+(new Date).getFullYear() ,
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      },
      maxDate: 0
    });
    $( "#to" ).datepicker({
      dateFormat: 'yy-mm-dd',
      defaultDate: "+1w",
      changeMonth: true,
	  changeYear: true,
      numberOfMonths: 1,
      yearRange: '2006:'+(new Date).getFullYear(),
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    }).datepicker("setDate", new Date()).datepicker( "option", "disabled", true );
  });
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
 
<label for="from">From</label>
<input type="text" id="from" name="from">
<label for="to">to</label>
<input type="text" id="to" name="to">

【讨论】:

  • 谢谢!但是,Date To 是否有可能只能选择当前日期?对不起:(
  • 当然。请重新检查一下。是你想要的吗?
  • 谢谢!这几乎是我需要的! :D
  • 还有什么?请您将问题标记为已解决好吗?
  • 我也喜欢,如果我只能接受这两个答案,但 balachandar 满足了我的大部分要求:(
猜你喜欢
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多