上面的答案绝对适用于这个问题。但是,没有触发按钮,我想出了一个解决方案,如下:
$('#txbxSocialHistoryDate').datepicker({
showButtonPanel: true,
changeMonth: true,
changeYear: true,
dateFormat: 'mm/dd/yyyy',
onSelect: function (dateText, inst) {
UIUtilities_jQueryDatePickerOnCloseEventIfAlsoMasked(this, 'txbxSocialHistoryDate', dateText);
},
onClose: function (dateText, inst) {
UIUtilities_jQueryDatePickerOnCloseEventIfAlsoMasked(this, 'txbxSocialHistoryDate', dateText);
}
});
$('#txbxSocialHistoryDate').mask("99/99/9999", { placeholder: " " });
function UIUtilities_jQueryDatePickerOnCloseEventIfAlsoMasked(thisDatePicker, txbxOfDatePicker, dateText) {
// when the calendar closes, determine if this was entered by hand, or by the calendar
var textInBox = $('#' + txbxOfDatePicker).val();
// if by the calendar, it will have 2 years attached, so we then shave the 2nd year off the end
// there is a brief flash of the incorrect year, but short of taking off both the masking & calendar on this one textbox, this will work...
if (textInBox.length > 10) {
$('#' + txbxOfDatePicker).val(dateText.substring(0, dateText.length - 4));
}
// this fixes the tabbing issue mentioned; now when you tab out of the calendar, it will move on to the appropriate control...
thisDatePicker.focus();
}
所以我做了什么:
- 声明日期选择器,但格式为:'mm/dd/yyyy',我错误地认为是它出现的样子。但是,在jQuery DateFormat 定义站点上,这实际上使日期在选择时被格式化为:mm/dd/yyyyyyyy(年份重复两次)。'yy' == DateFormat 中的 4 位数年份,显然。这个最初的错误让我找到了答案。
- 屏蔽您已经将日期选择器绑定到的相关日期字段。
- 使用更长的日期条目,删除我的面具的触发事件将停止。我所要做的就是放入我在 datepicker 的 onSelect 和 onClose 函数上创建的自定义方法中,现在一切正常。它基本上从日期选择器的选择中去除了不必要且不正确的双年。掩码和具有 4 位数年份格式的日期选择器现在可以一起使用。