javascript - datepicker beforeShowDay 返回 3 个月

标签 javascript jquery datepicker

我需要禁用 jquery datepicker 日历中的某一天,因此在 beforeShowDay 函数中我这样写:

beforeShowDay: function(date){
            if(parseInt(calMonth) != parseInt(date.getMonth())){
                calMonth = date.getMonth();
                alert(calMonth + ' - ' + date.getMonth());
            }
            return {0: true};
        }

其中 calMonth 包含当前月份数字。现在,如果我运行此命令,我会收到 3 个按顺序显示的警报: 9-9、10-10 和 11-11。为什么我有 3 条消息,虽然它不应该向我显示任何内容(因为当我打开 datepicker 时,它默认显示当月的日历,所以 if(parseInt(calMonth) != parseInt(date.getMonth())) 应该返回错误的。 我还设置了 numberOfMonths: 1。

最佳答案

有同样的问题,这就是我解决的方法:

$("#datepicker").datepicker({ showWeek: true, showOtherMonths : false,
    //I'm using onChangeMonthYear to always keep my datepicker month updated
    onChangeMonthYear: function(year, month, inst){
        $(this).datepicker( "setDate", month + '/1/' + year );
    },
    beforeShowDay: function(date) {
        //Now I can get only the days of the current selected month, and do whatever I want...
        if(date.getMonth()==$(this).datepicker('getDate').getMonth()) 
            console.log(date.getDate()+' - '+date.getMonth()+' - '+$(this).datepicker('getDate').getMonth());

        //keep returning as usual to the datepicker
        return [true, ''];
    } 
});

希望有帮助 =)

关于javascript - datepicker beforeShowDay 返回 3 个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286443/

相关文章:

angularjs - 如何设置ui bootstrap datepicker的日期值

ruby-on-rails - Bootstrap 日期选择器 rails 不工作

javascript - Knockoutjs 中的数字输入和范围 valueUpdate

javascript "this"未能定位

JavaScript 代码仅在文件保存在特定位置时运行

javascript - 使用 JavaScript/jQuery 的批量表单控件

javascript - 如何创建 CSS?

javascript - 通过按住鼠标按钮选择/取消选择复选框

javascript - 使用 Slick Scroller Carousel 隐藏自定义按钮

javascript - 母版页中的 Jquery 日期选择器