计数器未更新,其值将替换为当前值

时间:2013-09-23 22:18:03

标签: javascript drop-down-menu html-form

我在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/

1 个答案:

答案 0 :(得分:0)

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

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

这里你要比较一个数组和一个数字,尽管它实际上有效,因为它们都将被转换为字符串,而[36]的字符串值是"36",它与字符串值相同36 "36"的{​​{1}},以混乱的方式完成:

if(arrCount == 36){

错字,你写了arrMonthSarted而不是arrMonthStarted

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

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