0

我在 html 表单中使用 4 个下拉列表。2 个下拉菜单代表活动的开始和结束月份,另外 2 个代表活动的开始和结束年份。我允许用户输入 3 年的历史记录,完成后,我提示用户转到下一部分。为了计算 3 年的历史记录,我计算了开始月份和结束月份之间的差值,并且每次都将其输入到计数器中(请注意,我使用的是数字而不是 Date 对象)。这些值被传递到我的数组中,但计数器没有更新。它只是被数组中的新值替换。谁能告诉我问题出在哪里?这是我的代码:

var arrMonthStarted = [];  //It stores the month that activity started
var arrMonthEnded = [];   //It stores the month that activity ended
var arrYearStarted = []; //It stores the year that activity started
var arrYearEnded = [];  //It stores the year that activity ended
function validatedropdowns1(){
var monthStarted = document.getElementById('idMonthStarted').value;   
var yearStarted = document.getElementById('idYearStarted').value;    
var monthEnded = document.getElementById('idMonthEnded').value;     
var yearEnded = document.getElementById('idYearEnded').value;     
arrMonthStarted.push(monthStarted);  
arrMonthEnded.push(monthEnded);     
arrYearStarted.push(yearStarted);    
arrYearEnded.push(yearEnded);
//Calculating the 3-year history
var count = 0;
if(yearStarted == yearEnded){
    if(monthEnded < monthStarted){
        var temp = monthEnded;
        monthEnded = monthStarted;
        monthStarted = temp;
    }
    var diffmonths = monthEnded - monthStarted;
    count = count + diffmonths;
}
//Take the difference between the years.
var subYears = yearEnded - yearStarted;
//If 1, just take the difference on the first 2 lines of the calendar 
if(subYears == 1){
    var subLine1 = 12 - monthStarted;
    var subLine2 = 12 - monthEnded;
    var finalLine2 = 12 - subLine2;
    var takeresult = subLine1 + finalLine2;
    count = count + takeresult;
}
//Follow case 1, but also add 12 months
if(subYears == 2){
    var subLine3 = 12 - monthStarted;
    var subLine4 = 12 - monthEnded;
    var finalLine3 = 12 - subLine4;
    var takeresult11 = subLine3 + finalLine4;
    var takeresult1 = 12 + takeresult11;
    count = count + takeresult1l; 
}
//add another 12 months (24 now) on step 1.
if(subYears == 3){
    var subLine5 = 12 - monthStarted;
    var subLine6 = 12 - monthEnded;
    var finalLine5 = 12 - subLine6;
    var takeresult22 = subLine5 + finalLine6;
    var takeresult2 = 24 + takeresult22;
    count = count + takeresult2; 
}
var arrCount = []; // array to hold the count var
arrCount.push(count); // push count into arrCount 

//print total months
for(var m = 0; m < arrCount.length; m++){
    alert("The array now has" + "" + "" + count + "" + "months");
}

if(arrCount == 36){
    alert("You have successfuly finished this section. Please go to the next section. Thank you.")
    document.getElementById('btnAdd').disable = true;
}

if(arrMonthEnded[arrMonthEnded.length - 1] - arrMonthStarted[arrMonthSarted.length] > 1){
    alert("There should not be a gap of more than a month in your 3 year activity. Fill in all the months and select from the list what you were doing each month. Thank you.")
}
}

另外,我试图测试结束日期和下一个开始日期之间的差距。例如,如果我输入 12 2011 作为结束日期,并输入 03 2012 作为下一个开始日期,我想看看是否有超过一个月的差距。我尝试了下面的代码,但它没有工作

if(arrMonthEnded[arrMonthEnded.length - 1] - arrMonthStarted[arrMonthSarted.length] > 1){
    alert("There should not be a gap of more than a month in your 3 year activity. Fill in all the months and select from the list what you were doing each month. Thank you.")
}

提前感谢您(KSFIDDLE http://jsfiddle.net/k4dNb/

4

1 回答 1

0

这个循环是没有意义的,因为数组只会有一项:

for(var m = 0; m < arrCount.length; m++){

在这里,您将数组与数字进行比较,尽管这实际上有效,因为两者都将转换为字符串,并且 is 的字符串值与[36]is"36"的字符串值相同,但36"36"以一种令人困惑的方式完成:

if(arrCount == 36){

错字,你写arrMonthSarted的不是arrMonthStarted

if(arrMonthEnded[arrMonthEnded.length - 1] - arrMonthStarted[arrMonthSarted.length] > 1){

此外,当您尝试访问数组最后一项之外的项目时,arrMonthStarted[arrMonthStarted.length]将始终返回。undefined

于 2013-09-23T22:31:26.957 回答