我使用以下功能来禁用日期选择器日历中从2011-02-13到2011-02-18的日期:
I use the following function to disable days from 2011-02-13 to 2011-02-18 in the date picker calendar:
function no_disabled_days(date){ dateStr1 = '2011-02-13'; dateStr2= '2011-02-18'; disabled_start_day = new Date(dateStr1); disabled_end_day = new Date(dateStr2); if(date >= disabled_start_day && date <= disabled_end_day){ return [false]; } return [true]; } $("#reserve_date").datepicker({ beforeShowDay: no_disabled_Days });例如,如果 dateStr1 ='2011-02-13', dateStr2 ='2011-02-18',则表示 2011-02 -13 至 2011-02-18 已被禁用.
For example, if dateStr1='2011-02-13', dateStr2='2011-02-18', the days from 2011-02-13 to 2011-02-18 are disabled.
自从我使用
if(date >= disabled_start_day && date <= disabled_end_day)(请注意' = '符号)
因此," 2011-02-13 "和" 2011-02-18 "被禁用.
so, '2011-02-13' and '2011-02-18' are disabled.
在 Chrome 浏览器中一切正常,但是,当我在 Firefox 中进行测试时,并未禁用确切的disable_start_date,即" 2011-02 -13 "未禁用,其他日子正常运行.为什么?
Things are working fine in Chrome browswer, however, when I test in Firefox, the exact disable_start_date is not disabled, that's '2011-02-13' is not disabled, other days are working properly. Why?
为什么禁用开始日期( 2011-02-13 )在Firefox中未处于禁用状态?
Why the disabled start date (2011-02-13) is not in disable status in firefox?
推荐答案您遇到了时区问题.创建日期对象时,本地时区会影响Date对象的时间部分,从而影响比较.解决方案是在创建Date对象时显式设置时间.
You're running into a timezone issue. When you create your date object, your local timezone is affecting the time component of the Date objects, which, in turn, affects the comparison. The solution is to explicitly set the time when you create the Date objects.
此外,在函数中使用变量的方式是创建额外的全局变量.您应该使用var关键字使它们在函数中本地化.
Additionally, the way you are using variables in your function is creating extra global variables. You should use the var keyword to make them local to the function.
这是更正的代码:
function no_disabled_days(date){ var dateStr1 = '2011-02-13T00:00:00'; var dateStr2= '2011-02-19T00:00:00'; var disabled_start_day = new Date(dateStr1); var disabled_end_day = new Date(dateStr2); if(date >= disabled_start_day && date <= disabled_end_day){ return [false]; } return [true]; } $("#reserve_date").datepicker({ beforeShowDay: no_disabled_days });还有一个可行的示例(已在Firefox和Chrome中测试):
And a working example (tested in Firefox and Chrome):
jsfiddle/ChrisMH/5jX6B/1/
更多推荐
jQuery UI datepicker奇怪的问题(在Firefox中发生)
发布评论