2017-09-03 71 views
1

兩個datepickers,工作人員正在進入一個工作日到日期選擇1(#from),然後在第二日起開始(q若要)。日期2必須總是在日期1中。週末不允許。jQuery的日期選擇器 - 設置1選擇器來限制活躍的一週第二選擇器的

工作示例的這一點,不包括週末,而不是讓過去的日期日期2,可在此琴:

https://jsfiddle.net/gLrumpo3/6/

<input id="from"> 
<input id="to"> 

$("#from").datepicker({ 
    defaultDate: new Date(), 
    minDate: new Date(), 
    beforeShowDay: $.datepicker.noWeekends, 
    onSelect: function(dateStr) 
    { 
     $("#to").val(dateStr); 
     $("#to").datepicker("option",{ minDate: new Date(dateStr)}) 
    } 
    }); 

$('#to').datepicker({ 
    defaultDate: new Date(), 
    beforeShowDay: $.datepicker.noWeekends, 
    onSelect: function(dateStr) { 
    toDate = new Date(dateStr); 
    fromDate = ConvertDateToShortDateString(fromDate); 
    toDate = ConvertDateToShortDateString(toDate); 
    } 
}); 

啥子我現在需要的雖然是要能夠「鎖定」第二個日期輸入到同一周的第一天,我想到了伊辛的maxDate,但我只能specifiy任意偏移,像Xdays,或特定的日期。這是不好的,因爲人們可能會選擇星期四例如,我不能然後添加4天,並將其設置爲最大日期,因爲他們將能夠選擇星期一下週。

那麼我怎樣才能約束自己的日期inouts到一個星期,在那個星期設置absed上選擇第一場的日子。

請如果可能的話工作答案更新小提琴。謝謝。

回答

2

試試這個代碼

var date = $(this).datepicker('getDate'); 
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6); 
$("#to").datepicker("option",{minDate: new Date(dateStr), maxDate:endDate}); 

您可以設置maxDate到本週的最後日期爲$("#to")日期選擇器。

$("#from").datepicker({ 
 
    defaultDate: new Date(), 
 
    minDate: new Date(), 
 
    beforeShowDay: $.datepicker.noWeekends, 
 
    onSelect: function(dateStr) 
 
    { 
 
     $("#to").val(dateStr); 
 
     var date = $(this).datepicker('getDate'); 
 
     endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6); 
 
     $("#to").datepicker("option",{minDate: new Date(dateStr), maxDate:endDate}); 
 
     
 
    } 
 
}); 
 

 
$('#to').datepicker({ 
 
    defaultDate: new Date(), 
 
    beforeShowDay: $.datepicker.noWeekends, 
 
    onSelect: function(dateStr) { 
 
    toDate = new Date(dateStr); 
 
    //fromDate = ConvertDateToShortDateString(fromDate); 
 
    //toDate = ConvertDateToShortDateString(toDate); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script> 
 
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<input id="from"> 
 
<input id="to">

+0

完美的作品,upvoted並回答接受。謝謝。 –

+0

很高興幫助你:) – Amal

相關問題