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.")
}
}
또한 끝 날짜와 다음 시작 날짜 사이의 간격을 테스트하려고했습니다. 예를 들어, 종료일로 2011 년을, 다음 시작일로 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/를) 감사 작동하지 않았다
[JSFiddle] (http://jsfiddle.net)으로 게시 할 수 있습니까? –
물론 ... 여기에 http://jsfiddle.net/k4dNb/ –