问题
I have two datepickers at the moment, one for a start date, and another for an end date.
When you select the first date, I'd like the default value of the second datepicker to be set to the same date. If I use the setDate
it works and sets the value just fine, but the default value won't change - if I set the defaultDate
when the datepicker is loaded, it works, but if I try to set it on the fly it doesn't.
Here's my code:
if($('#startDate').val().length == 10 && $("#endDate").val().length != 10) {
$("#endDate").datepicker("defaultDate", $("#startDate").datepicker("getDate"));
}
(edit) for clarification, I don't want to set the date - the user needs to do that, but I'd like the datepicker to go straight to the same date as the start date, to save them having to trawl through numerous pages.
Here's the (kinda) working code: http://jsfiddle.net/c8H5y/
Any suggestions would be very welcome, thank you!
回答1:
This should do it:
$("#dep_date").datepicker({
dateFormat: 'dd/mm/yy',
onSelect: function(dateText, inst) {
var date = $.datepicker.parseDate('dd/mm/yy', dateText);
var $ret_date = $("#ret_date");
$ret_date.datepicker("option", "defaultDate", date);
}
});
$("#ret_date").datepicker({
dateFormat: 'dd/mm/yy',
});
jsFiddle
来源:https://stackoverflow.com/questions/7598710/jquery-datepicker-defaultdate-dynamically-from-another-datepicker