问题
So I have a jQuery DateTimePicker v2.4.1 attached on a text input. I wanted to make it so then when the user clicks and changes the Month or Year of the picker - it would update the date within the form as this doesn't seem to be a default behavior of the plugin.
$(timepicker).datetimepicker({
format: "d M Y h:i A",
step: 15,
validateOnBlur: false, //use ValidateDateTime.js instead of jquery default
closeOnInput: true,
closeOnDateSelect: true,
onClose: function() {
var validatedDate = validateDate($(timepicker).val());
if (validatedDate != false) {
$(timepicker).val(validatedDate);
}
},
changeMonth: true,
changeYear: true,
onChangeMonth: function() {
//pop name of month into field and update calendar
var d = $(this).datetimepicker('getDate');
console.log(d + this.html());
/*
$(timepicker).datetimepicker.trigger("select.xdsoft", [d]);
*/
}
});
A problem that occurs is: var d = $(this).datetimepicker('getDate');
does not actually retrieve the date and just returns the datetimepicker element.
How do I get the date in js or more specifically - how could I force an update when the user clicks on a month within the drop down.
EDIT: Looking at the documentary http://xdsoft.net/jqplugins/datetimepicker/ & thanks to suish it now looks like this:
onChangeMonth: function(date, $el) {
//pop name of month into field and update calendar
var str = dateTimeToString(date);
$($el).datetimepicker({ value: str });
},
Simple!
回答1:
onChangeMonth function(current_time,$input){}
onChangeYear function(current_time,$input){}
onChangeDateTime function(current_time,$input){}
According to its documentation.each onChange functions get Date variable as a first parameter.
onChangeMonth: function(date,$el) {
var d = date.getDate();
//d is selected date.
var m = date.getMonth() + 1; //+1 because .getMonth() returns 0-11
//m is selected month.
}
来源:https://stackoverflow.com/questions/29201624/jquery-datetimepicker-how-to-get-date-in-javascript