[英]Show wrong month and year when refocused same datepicker input with using jquery ui datepicker
I want to show only month and year. 我想只显示月份和年份。 If I refocused same input after select a date, it shows wrong month and year not show previously selected month and year.
如果我在选择日期后重新聚焦相同的输入,则显示错误的月份和年份未显示先前选择的月份和年份。 For example I select December 2010 and then refocused same input it shows April 2015 as below image.
例如,我选择2010年12月,然后重新聚焦2015年4月显示的相同输入,如下图所示。 How can I?
我怎样才能?
HTML HTML
<input type="text" class="dp" readonly="true">
CSS CSS
.ui-datepicker-calendar {
display: none;
}
JS JS
$('.dp').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'mm.yy',
onClose: function (dateText, inst) {
$(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay));
}
});
The datepicker expects to be able to parse the displayed version of the date into a javascript date object. datepicker期望能够将显示的日期版本解析为javascript日期对象。
One way to do this is to add a custom parseDate function for your special date format. 一种方法是为您的特殊日期格式添加自定义parseDate函数。 The code below looks for the special format of "mm.yy" and if found, parses the value into a date.
下面的代码查找“mm.yy”的特殊格式,如果找到,则将值解析为日期。 If the format is anything else, pass processing to the original datepicker.parseDate.
如果格式是其他任何内容,则将处理传递给原始的datepicker.parseDate。
var standardParseDate = $.datepicker.parseDate;
function customParseDate( format, value, options ) {
if (format === 'mm.yy') {
var dt = (value + ".").split(".");
var month = parseInt(dt[0]) - 1;
var year = parseInt(dt[1]);
if (year < 100) {
year += 2000;
}
if (isNaN(month) || isNaN(year)) {
return null;
}
return new Date(year, month, 1);
} else {
return standardParseDate(format,value,options);
}
}
$.datepicker.parseDate = customParseDate;
JSFiddle: http://jsfiddle.net/3drfd35t/ JSFiddle: http : //jsfiddle.net/3drfd35t/
additional code for customParseDate: customParseDate的其他代码:
} else if (format === 'M-yy') {
var datestr = value;
if (datestr.length > 0) {
var y = datestr.substring(datestr.length - 4, datestr.length); // last 4 chars
var m = jQuery.inArray(datestr.substring(0, datestr.length - 5), options.monthNamesShort);
var newDate = new Date(y, m, 1);
console.log("customParseDate " + "m:" + m + ", y:" + y); // dbg
return newDate;
} else {
console.log("customParseDate " + "null"); // dbg
return null;
}
} else . . .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.