2015-11-05 83 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