如何在JavaScript中修复此For循环

时间:2015-01-06 13:03:41

标签: javascript arrays for-loop datepicker jquery-ui-datepicker

以下是附加了输出图像的代码。如何修复它只显示一次datepicker。并且只需显示最后一个日历,其上突出显示所有选定日期。

   for (var ind = start.getMonth(); ind <= yearDifference; ind++) {

            calendarForThisMonth.push(monthNames[ind % 12]);
            calendarForThisYear.push(Math.floor(startYear + (ind / 12)));
            var showCalendarForThisMonth = calendarForThisMonth.pop();
            var showCalendarForThisYear = calendarForThisYear.pop();
            saveEachMonthIndex.push(showCalendarForThisMonth);
            saveEachYearIndex.push(showCalendarForThisYear);
            //console.log(showCalendarForThisMonth);

            var line = $('<div class="showInGrid" id="removeMeLater"><span class="dateP" id="calendar' + ind + '" ></span></div>');
            //$('#myCalendar').append(line);
            var selectedDates={};
            for(var eachDateOfMonth in selected) {

                if(new Date(selected[eachDateOfMonth]).getMonth() == monthNames[ind%12]) {
                    selectedDates[new Date(selected[eachDateOfMonth])] = new Date(selected[eachDateOfMonth]);
                    console.log(selectedDates[new Date(eachDateOfMonth)]);

                    dateObj.setMonth(showCalendarForThisMonth);
                    dateObj.setYear(showCalendarForThisYear);

                    var line = $('<div class="showInGrid" id="removeMeLater"><span class="dateP" id="calendar' + ind + '" ></span></div>');

                    $('#myCalendar').append(line);
                    line.find("#calendar" + ind).datepicker({defaultDate:dateObj,
                        beforeShowDay: function (dateToHighlight) {
                            var Highlight = selectedDates[dateToHighlight];
                            if (Highlight) {
                                return [true, "Highlighted", Highlight];
                            }
                            else {
                                return [true, '', ''];
                            }
                        }
                    });delete dateObj;

                }
            }


            var saveId = $("#calendar"+ind).attr("id");
            console.log(saveId);

            }

enter image description here

0 个答案:

没有答案