首页 > 解决方案 > 为什么此警报显示“未定义”而不是当前月份的值?

问题描述

当我执行此代码时,警报会显示“未定义”而不是当前月份的值

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);
}

标签: javascriptundefined

解决方案


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

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


推荐阅读