javascript - 单击 ajax 后无法重新加载 fullcalendar div

标签 javascript html css ajax fullcalendar

我正在通过 fullcalendar 构建日历.

我制作了一个 ajax 按钮,它将从另一个 php 页面获取事件。

ajax 按钮工作正常,我能够获取带有事件的 json,并且我得到了一个漂亮的月历显示(在我第一次点击 ajax 按钮时)。

我唯一的问题是:当我点击 ajax 按钮上的第二个时间时,日历不会重新加载/重新发布/呈现..并且我停留在第一个日历上(这对我来说是无关紧要的)。

任何人都知道问题是什么,我该怎么做才能在每次单击 ajax 按钮时重新发布新日历?

代码如下:

<form id="my-form-id" method="post" action="Calendar_form3.php"> Year:
    <INPUT NAME="gregorian_year" SIZE=4 MAXLENGTH=4 value="2016">(1-6000) Latitude:
    <INPUT NAME="latitude" value="40.7127"> Longitude:
    <INPUT NAME="longitude" value="-74.0059"> <button id="ajax-button" type="button">Ajax button</button> </form>
<script>
    function displayCalendar() {
        var target = document.getElementById("main");
        var formData = new FormData(document.getElementById("my-form-id"));
        console.log(formData);
        var xhr = new XMLHttpRequest();
        xhr.open('post', 'JewishCalendar_form3.php', true);
        xhr.onreadystatechange = function() {
            console.log('readyState: ' + xhr.readyState);
            if (xhr.readyState == 2) {
                target.innerHTML = 'Loading...';
            }
            if (xhr.readyState == 4 && xhr.status == 200) {
                var Month_details_for_display = JSON.parse(xhr.responseText);
                //var Month_details_for_display= JSON.parse(Month_details_for_display2);
                target.innerHTML = "funciton will start working...";
                displayCalendar222(Month_details_for_display);
            }
        }
        xhr.send(formData);
    }

    function displayCalendar222(Month_details_for_display) {
        alert("Hello! I am an alert box!!");
        var Events_In_Json = Month_details_for_display['EventsInSameStractureForAll'];
        var json_backgrundColor = Month_details_for_display['Big_Calendar_cell_background_color'];
        var json_iconstring = Month_details_for_display['iconString'];
        var DefaulteDateForFullCalendarISO8601 = Month_details_for_display['fullMonthDetails']['DefaulteDateForFullCalendarISO8601'];
        $('#calendar').fullCalendar({
            header: {
                left: 'prev',
                center: 'title',
                right: 'next'
            },
            events: Events_In_Json,
            fixedWeekCount: false,
            defaultDate: DefaulteDateForFullCalendarISO8601,
            dayRender: function(date, cell) {
                var cellDate = date.format('D');
                if (!cell.hasClass('fc-other-month')) {
                    //if this if is true that means that the day belongs to the current relevent month (and not the prev \ next month)
                    cell.css('background-color', json_backgrundColor[cellDate]);
                    //from here: cheking which icons to show
                    if (json_iconstring[cellDate].includes('HAV')) {
                        cell.prepend('<img src=\' icons/havdala2.png \'>');
                    }
                    if (json_iconstring[cellDate].includes('Mod')) {
                        cell.prepend('<img src=\' icons/israel.png \'>');
                    }
                    if (json_iconstring[cellDate].includes('Jewish')) {
                        cell.prepend('<img src=\' icons/jewish.png \'>');
                    }
                    if (json_iconstring[cellDate].includes('PAR')) {
                        cell.prepend('<img src=\' icons/parasha.png \'>');
                    }
                    if (json_iconstring[cellDate].includes('CL')) {
                        cell.prepend('<img src=\' icons/cl.png \'>');
                    }
                    if (json_iconstring[cellDate].includes('Pub')) {
                        if (json_iconstring[cellDate].includes('USA')) {
                            cell.prepend('<img src=\' icons/usa.png \'>');
                        } else if (json_iconstring[cellDate].includes('UK')) {
                            cell.prepend('<img src=\' icons/uk.png \'>');
                        } else if (json_iconstring[cellDate].includes('CA')) {
                            cell.prepend('<img src=\' icons/canada.png \'>');
                        } else if (json_iconstring[cellDate].includes('AUS')) {
                            cell.prepend('<img src=\' icons/australia.png \'>');
                        } else if (json_iconstring[cellDate].includes('FR')) {
                            cell.prepend('<img src=\' icons/france.png \'>');
                        }
                    }
                    //until here:: cheking which icons to show
                } else {
                    //this days belongs to the prev \ next months. so we give them opacity)        
                    cell.css('background-color', '#ffffff');
                }
            },
        });
    }
    var button = document.getElementById("ajax-button");
    button.addEventListener("click", displayCalendar);
</script>
<div id='main'> result here </div>
<div id='calendar'></div>

最佳答案

你第二次点击按钮时,你应该调用类似的东西:

$('#calendar').fullCalendar('render');

或者在再次渲染之前完全销毁压延机,比如:

$('#calendar').fullCalendar('destroy')
              .empty()
              .fullCalendar({
                 //...
              });

关于javascript - 单击 ajax 后无法重新加载 fullcalendar div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41116346/

相关文章:

javascript - 如何从 JavaScript 返回一个值到页面加载函数

javascript - 为什么我的 jquery 切换类无法单击

php - 切换标签菜单

html - 绝对定位容器中的 Flexbox - 宽度是最宽列的宽度,而不是所有内容

javascript - 使用模板使用的所有 CSS 规则生成一个内置的 angular index.html 页面?

html - 在 <blockquote> 中包裹一条长线,使这些线的长度相等

javascript - 特定元素上的忙碌指示器(Wicket)

javascript - python中的零填充右移

javascript - 如何比较 JavaScript 中的事件

javascript - 未捕获( promise )错误 : The query requires an index