为什么此警报显示的是“未定义”而不是当月的值?

时间:2019-07-31 12:43:37

标签: javascript undefined

当我执行此代码时,警报显示为'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);
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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