问题
I try to create a date range by using the jQuery UI datepicker, using two text fields. The first text field "start_date" will set the start date and the second text field "end_date" will set the end date.
The code I have till now is this:
$('#start_date').live(
'focus',
function()
{
$('#end_date').datepicker(
{
dateFormat: 'dd MM yy',
minDate: new Date(),
maxDate: new Date(2012, 9, 15),
stepMonths: 2,
numberOfMonths: 2
}
);
$(this).datepicker(
{
dateFormat: 'dd MM yy',
minDate: new Date(),
maxDate: new Date(2012, 9, 15),
stepMonths: 2,
numberOfMonths: 2,
onSelect: function(dateText, inst)
{
var instance = $( this ).data("datepicker");
var date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);
$('#end_date').datepicker('option', 'minDate', dateText);
}
}
);
}
);
NOTE : I use the live and the focus event because all my page content is loaded with AJAX call. Maybe that is correct or wrong, but this is not what I like to ask here ;)
The above code is correct for me and work fine, but what I like to do, is to set the value of the 'end_date' element to selected one +3 days.
Until now, when I choose a date at the "start_date" element the "end_date" element change to the same date as the "start_date", and this is what I like to change. The "end_date" to be +3 days from the "start_date" element adter the selection.
How can I do that ?
回答1:
To set end_date for +1 day
onSelect: function(dateText, inst) {
$('#start_date').datepicker('option','minDate', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay));
var toDate = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);//Date one month after selected date
var oneDay = new Date(toDate.getTime()+86400000);
document.getElementById('end_date').value =$.datepicker.formatDate('dd/mm/yy', oneDay);
}
回答2:
Hiya demo http://jsfiddle.net/96Lbf/
This demo will set to
text box with +3 days
taking `from date selection into account.
jquery code
$(function() {
$( "#from, #to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onSelect: function( selectedDate ) {
if(this.id == 'from'){
var dateMin = $('#from').datepicker("getDate");
var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1);
var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 3);
//$('#to').datepicker("option","minDate",rMin);
//$('#to').datepicker("option","maxDate",rMax);
$('#to').val($.datepicker.formatDate('mm-dd-yy', new Date(rMax)));
}
}
});
});
Html
<div class="demo">
<label for="from">From</label>
<input type="text" id="from" name="from"/>
<label for="to">to</label>
<input type="text" id="to" name="to"/>
</div>
回答3:
If month = 4, I want to see car1 picture. If month = 5, i want to see car3 picture. How can I do it?
//..............
onSelect: function( selectedDate ) {
if(this.id == 'from'){
var dateMin = $('#from').datepicker("getDate");
var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 0);
alert (rMin.getMonth());
if(rMin.getMonth() == 4){
var image1=new Image();
image1.src="car1.jpg"
if(rMin.getMonth() ==5){
var image2=new Image()
image2.src="car3.jpg";
}
}
}
}
<label for="from">From</label>
<input type="text" id="from" name="from"/>
<label for="pics">pics</label>
<input type="image"
src="car1.jpg"
id="pics"
name="pics"
alt="Submit"
width="48" height="48">
...............
来源:https://stackoverflow.com/questions/10530134/jquery-ui-datepicker-onselect-get-the-selected-date-3-days