startDate在FullCalendar插件中显示两次

startDate shows twice in FullCalendar plugin

本文关键字:两次 显示 FullCalendar 插件 startDate      更新时间:2023-09-26

我正在为我的一个项目使用FullCalendar插件。当用户单击日历的一个区域时,它会显示一个带有输入和约会按钮的弹出框。当用户单击Appointment按钮时,调用makeAppointment函数,我只需将startDate回显到控制台。

当用户第一次单击Appointment按钮时,它记录所选择的日期和时间。当用户选择"第二个日期和时间",并在弹出窗口点击约会按钮,它显示两个日期和时间,即一个以前的日期和时间和一个当前选择的日期和时间。第三次和第四次也是一样。为什么它有这种行为,我如何修复它?

我的代码

var Calendar = {
    init: function () {
        $('#calendar').fullCalendar({
            defaultView: 'agendaWeek',
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'agendaWeek,agendaDay',
                ignoreTimezone: false
            },
            select: this.select
        });
    },
    select: function (startDate, endDate, allDay, jsEvent, view) {
        Calendar.Dialog.init(startDate, endDate);
    },
    Dialog: {
        init: function (startDate, endDate) {
            this.show();
            $('.overlay').on('click', function () { Calendar.Dialog.close() });
            $('#appointmentButton').on('click', function () { Calendar.Dialog.makeAppointment(startDate, endDate) });
        },
        //show and close functions are here
        makeAppointment: function (startDate, endDate) {
            console.log(startDate);
        }
    }
}

尝试先检查对话框是否已初始化,否则不要再做。

    var Calendar = {
    init: function () {
        $('#calendar').fullCalendar({
            defaultView: 'agendaWeek',
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'agendaWeek,agendaDay',
                ignoreTimezone: false
            },
            select: this.select
        });
    },
    initialized: false,
    select: function (startDate, endDate, allDay, jsEvent, view) {
        if (this.initialized === false) {
            this.initialized = true;
            Calendar.Dialog.init(startDate, endDate);
        }
    },
    Dialog: {
        init: function (startDate, endDate) {
            this.show();
            $('.overlay').on('click', function () { Calendar.Dialog.close() });
            $('#appointmentButton').on('click', function () { Calendar.Dialog.makeAppointment(startDate, endDate) });
        },
        //show and close functions are here
        makeAppointment: function (startDate, endDate) {
            console.log(startDate);
        }
    }
}