2013-09-23 2 views
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.") 
} 
} 

또한 끝 날짜와 다음 시작 날짜 사이의 간격을 테스트하려고했습니다. 예를 들어, 종료일로 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/를) 감사 작동하지 않았다

+0

[JSFiddle] (http://jsfiddle.net)으로 게시 할 수 있습니까? –

+0

물론 ... 여기에 http://jsfiddle.net/k4dNb/ –

답변

0

배열이 오직 하나 개의 항목이되므로이 루프는 무의미 :

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

여기서는 배열을 숫자와 비교합니다. 두 문자열이 모두 문자열로 변환되고 [36]의 문자열 값이 "36"이고 문자열 값이인 경우 실제로 작동합니다. "36" 인 953,210, 혼란 방법으로 이루어집니다 : 당신이 액세스하려고으로 또한

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

arrMonthStarted[arrMonthStarted.length] 항상 undefined를 반환합니다

if(arrCount == 36){ 

오타, 당신은 arrMonthSarted 대신 arrMonthStarted을 썼다 항목을 배열의 마지막 항목보다 우선합니다.

+0

답변 주셔서 고맙습니다. 나는 그것이 혼란 스럽다는 것을 안다. 순수한 숫자로 작업하는 날이 훨씬 미쳐 버리기 때문이다. 그 달 사이의 격차는 무엇을 제안합니까? –

+0

@ AndreUk13 : 가지고있는 색인에서 하나를 뺀다.하지만 'arrMonthEnded'에 적어도 두 항목이 포함되어 있는지 먼저 확인하여 이전 종료일을 확인한다. 게다가 여러 해를 다룰 수 있도록 여러 해를 가치에 통합해야합니다. – Guffa

+0

예. 누군가 2012 년 1 월 1 일과 2013 년 1 월을 놓는다면 평가가되지 않을 것입니다. 그래서 많든 적든 : if (arrMonthEnded> = 2) {if (arrMonthEnded [arrMonthEnded.length - 1] - arrMonthStarted [arrMonthSarted.length -1]> 1) {// 여기 코드}}). 당신의 도움을 많이 주셔서 고마워요. –

관련 문제