jQuery UI Datepicker Problem with arrival - depart dates
Argh.. I am stumped....
I have a reservation module that is running the jQuery date-picker UI. I have an arrival date field and a departure date field each with a date-picker object initiated on both.
I want the default arrival date to be todays date I achieved this by doing the following:
<script type="text/javascript">
$(document).ready(function(){
$(".arrive").datepicker();
$(".depart").datepicker();
$(".arrive").datepicker('setDate', new Date());
});
</script>
That works perfectly however when a user selects an arrival date I then want the departure default date to be set as one day after the users selected arrival date. I tried using the onSelect events for the date-picker object and tried to pull the value from the arrival field and add a day but to no avail. I have seen this functionality before, I don't think it is very difficult I am just beyond stumped on this one. Any ideas? My html for the text fields is as follows:
<ul>
<li>
<label>Arriving</label>
<span class="dateinput"> 开发者_高级运维
<input class="arrive" type="text" value="" name="startDate" />
<img id="arrive" src="images/calendar_icon.png" />
<div class="clear"></div>
</span>
</li>
<li>
<label>Departing</label>
<span class="dateinput">
<input class="depart" name="endDate" value="" type="text" />
<img src="images/calendar_icon.png" />
<div class="clear"></div>
</span>
</li>
</ul>
Thanks a bunch to whoever can help me with this issue.
Using the documentation as inspiration:
<script type="text/javascript">
$(document).ready(function(){
$(".arrive").datepicker({
onSelect: function( selectedDate ) {
// Parse the selected date
var instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
// Add one day
date.setDate(date.getDate()+1);
// Set the new date
$(".depart").datepicker('setDate', date);
}
});
$(".depart").datepicker();
$(".arrive").datepicker('setDate', new Date());
});
</script>
did you have a look at the documentation?
http://jqueryui.com/demos/datepicker/#date-range
-> view source
it even got some onSelect-code you could partly use for your specific problem
after some trial and error found this worked well for me to set a min departure date and max arrival date (in case departure selected before arrival)
$("#arrival").datepicker({
//set other options here
minDate: 0, //sets min date today
onClose: function() {
let arriveDate = $(this).datepicker("getDate");
if(arriveDate){
arriveDate.setDate(arriveDate.getDate() + 1);
$("#depart").datepicker("option", "minDate", arriveDate); //sets min departure date to arrival date + 1
}else $("#depart").datepicker("option", "minDate", 1); //reset to default min departure date (in case arrival date was earlier set and later removed)
}
})
$("#depart").datepicker({
//set other options here
minDate: 1, //sets min date today + 1
onClose: function() {
let departDate = $(this).datepicker("getDate");
if(departDate){
departDate.setDate(departDate.getDate() - 1);
$("#arrival").datepicker("option", "maxDate", departDate); //sets max arrival date to departure date - 1
}else $("#arrival").datepicker("option", "maxDate", ''); //reset to default max arrival date (in case departure date was earlier set and later removed)
}
})
精彩评论