为什么此警报显示 'undefined' 而不是当前月份的值?

Why does this alert show me 'undefined' instead of current month's value?

当我执行此代码时,警报显示 'undefined' 而不是当前月份的值

function jump() {
    selectYear = document.getElementById("start"); // used for jump section
    selectYear = selectYear.value.toString().slice(0,4);
    selectMonth = document.getElementById("start"); // used for jump section
    selectMonth = selectMonth.value.toString().slice(5,7);
    currentYear = selectYear.value;
    currentMonth = selectMonth.value;
    alert (currentMonth);
    window.value = 0;
    showCalendar(currentMonth, currentYear);
}

如评论中所述,您需要将其从字符串解析为整数。 selectMonth 是一个字符串,因此它没有 .value 助手。

您可以使用 parseInt() 函数来解决此问题,而不是使用 .value 帮助程序。

function jump() {

    selectYear = document.getElementById("start").value;
    selectYear = selectYear.value.toString().slice(0,4);
    selectMonth = document.getElementById("start").value;

    selectMonth = selectMonth.value.toString().slice(5,7);
    currentYear = selectYear.value;
    currentMonth = selectMonth.value;

    alert (currentMonth);
    window.value = 0;
   showCalendar(currentMonth, currentYear);
}