2015-11-05 84 views
0

我有两个日期,从日期和日期到,我的问题是,我希望从日期只能点击/选择过去的日期,直到当前的日期,然后日期可以自动选择当前日期基于当前日期的日期验证

下面的代码

<!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 \t w", 
 
     changeMonth: true, 
 
\t 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, 
 
\t 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>

+0

可能重复的[发布](http://stackoverflow.com/questions/14810602/how-to-set-mindate-to-current-date-in -jquery-UI-日期选择器)。 – Rajesh

+0

我试过了,它不起作用:( – Vista

+0

,它也不同 – Vista

回答

2

使用的maxDate属性就可以达到您的要求。

请将DateDicker的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

+0

谢谢!,日期从工作很好,但我希望在日期至它自动选择当前日期 – Vista

+0

@Vista试试这个小提琴链接http ://jsfiddle.net/1421knt5/3/ 现在我将使用** setDate **方法为日期选择器设置日期 – balachandar

+0

是否有可能,该日期不能选择任何东西只有当前日期?对不起:( – Vista

1

$(function() { 
 
    $("#from").datepicker({ 
 
     dateFormat: 'yy-mm-dd', 
 
     defaultDate: "+1 \t w", 
 
     changeMonth: true, 
 
\t 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, 
 
\t 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">

+0

谢谢!但是,有可能,日期不能选择任何东西只有当前日期?对不起:( – Vista

+0

当然。请重新检查它。这是你想要的吗? –

+0

谢谢!这几乎是我需要的! :D – Vista