当我破解“今天”时,JQuery-UI 日期选择器出现错误按钮也可以选择日期,但仅限于 IE

发布于 2024-12-29 01:11:51 字数 2078 浏览 4 评论 0原文

我使用了各种“修复”来使“今天”按钮也可以选择日期,到目前为止,它们在 Firefox 上运行良好,但当我使用它们时 IE 会出现错误。每当我单击“今天”时,都会选择日期,更新输入字段并关闭日期选择器,但 IE 会立即打开另一个日期选择器,除非您打开一个新的日期选择器,否则该日期选择器无法关闭。

这是带有我使用的修复程序的日期选择器代码

var $j = jQuery.noConflict();            
            $j('#data1, #data2').datepicker({ dayNames: ['Duminica', 'Luni', 'Marti', 'Miercuri', 'Joi', 'Vineri', 'Sambata'], dayNamesMin: ['Du', 'Lu', 'Ma', 'Mi', 'Jo', 'Vi', 'Sa'], firstDay: 1,
                monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie'], 
                monthNamesShort: ['Ian','Feb','Mar','Apr','Mai','Iun','Iul','Aug','Sep','Oct','Noi','Dec'],
                showOtherMonths: true,
                selectOtherMonths: true,            
                dateFormat: 'yy-mm-dd',
                defaultDate: new Date(),
                changeMonth: true,
                showButtonPanel: true, currentText: 'Astazi',
                changeYear: true    
            });

我使用的第一个修复程序:

$j('button.ui-datepicker-current').live('click', function() {
$j.datepicker._curInst.input.datepicker('setDate', new Date()).datepicker('hide');
});

在初始化日期选择器之后。

还重写了该函数

var _gotoToday = jQuery.datepicker._gotoToday;
// datepicker is directly inside the jQuery object, so override that
jQuery.datepicker._gotoToday = function(a){
var target = jQuery(a);
var inst = this._getInst(target[0]);
// call the old function, so default behaviour is kept
_gotoToday.call(this, a);
// now do an additional call to _selectDate which will set the date and close
// close the datepicker (if it is not inline)
jQuery.datepicker._selectDate(a, 
jQuery.datepicker._formatDate(inst,inst.selectedDay, inst.selectedMonth,  inst.selectedYear));
}

添加

this._setDateDatepicker(target, new Date());
this._selectDate(id, this._getDateDatepicker(target));

还尝试在 _gotoToday 函数的末尾

所有这些函数都完全符合您在 Firefox 中的预期,但在与 IE 一起使用时给我留下了上面提到的问题。

I've used various "fixes" to make the today button also select the date and so far all of them worked nicely on Firefox but IE bugs out when I use them. Whenever I click today, the date gets selected, the input field updated and the datepicker closed but IE imediately opens another datepicker that can't be closed unless you open a new datepicker.

This is the datepicker code with the fixes I used

var $j = jQuery.noConflict();            
            $j('#data1, #data2').datepicker({ dayNames: ['Duminica', 'Luni', 'Marti', 'Miercuri', 'Joi', 'Vineri', 'Sambata'], dayNamesMin: ['Du', 'Lu', 'Ma', 'Mi', 'Jo', 'Vi', 'Sa'], firstDay: 1,
                monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie'], 
                monthNamesShort: ['Ian','Feb','Mar','Apr','Mai','Iun','Iul','Aug','Sep','Oct','Noi','Dec'],
                showOtherMonths: true,
                selectOtherMonths: true,            
                dateFormat: 'yy-mm-dd',
                defaultDate: new Date(),
                changeMonth: true,
                showButtonPanel: true, currentText: 'Astazi',
                changeYear: true    
            });

The first fix I used:

$j('button.ui-datepicker-current').live('click', function() {
$j.datepicker._curInst.input.datepicker('setDate', new Date()).datepicker('hide');
});

just after I initialize the datepicker.

Also rewrote the function with

var _gotoToday = jQuery.datepicker._gotoToday;
// datepicker is directly inside the jQuery object, so override that
jQuery.datepicker._gotoToday = function(a){
var target = jQuery(a);
var inst = this._getInst(target[0]);
// call the old function, so default behaviour is kept
_gotoToday.call(this, a);
// now do an additional call to _selectDate which will set the date and close
// close the datepicker (if it is not inline)
jQuery.datepicker._selectDate(a, 
jQuery.datepicker._formatDate(inst,inst.selectedDay, inst.selectedMonth,  inst.selectedYear));
}

Also tried just adding

this._setDateDatepicker(target, new Date());
this._selectDate(id, this._getDateDatepicker(target));

at the end of the _gotoToday function

All of them did exactly what you'd expect in firefox but left me with the issue I mentioned above when used with IE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2025-01-05 01:11:51

对我来说,这个也适用于 IE:

jQuery.datepicker._gotoToday = function(id) {
    var today = new Date();
    var dateRef = jQuery("<td><a>" + today.getDate() + "</a></td>");
    this._selectDay(id, today.getMonth(), today.getFullYear(), dateRef);
};

For me, this one works, also in IE:

jQuery.datepicker._gotoToday = function(id) {
    var today = new Date();
    var dateRef = jQuery("<td><a>" + today.getDate() + "</a></td>");
    this._selectDay(id, today.getMonth(), today.getFullYear(), dateRef);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文