2014-03-03 2 views
0

jquery 퀴즈를 쓰고 있습니다.Jquery가 작동하지 않으면 else를 얻을 수 없습니다.

먼저 나는 페이지에 따라 변수를 설정 해요 : 그들은 질문에 대답 할 때

if ($("body").hasClass("questionone")){ 
    var $currentQuestion = 1; 
    console.log($currentQuestion); 
    } 
    else if ($("body").hasClass("questiontwo")){ 
    var $currentQuestion = 2; 
    console.log($currentQuestion); 
    } 
    else if ($("body").hasClass("questionthree")){ 
    var $currentQuestion = 3; 
    console.log($currentQuestion); 
    }; 

은 그럼 내가 잘못되거나 올바른 말한다 팝 위로 다음 질문을라는 버튼이 있습니다. 다음 질문 버튼은 currentQuestion 변수가 무엇인지에 따라 방향을 재 지정합니다. 문제는 작동되는 경우 부분 다시 지시 클릭에 2를 질문하지만 질문 2에 여기

3. 리디렉션 코드에 의문을 리디렉션하지 않는 질문 1 :

$(".nextquestion").on("click", function(){ 
    if($currentQuestion = 1) { 
     window.location.href = "question2.html"; 
    } 
    else if($currentQuestion = 2) { 
     console.log("thisworks"); 
     window.location.href = "question3.html"; 
    } 
    else if($currentQuestion = 3) { 
     window.location.href = "question4.html"; 
    }; 
    }); 

도움 주셔서 감사합니다.

if($currentQuestion === 1) { 
... 
else if($currentQuestion === 2) { 
... 
else if($currentQuestion === 3) { 

을 그리고 var 선언 (var $currentQuestion = 2;)를 반복하지 않도록하십시오 그것을 해독 코드가 하드하게, : 당신이 할당하고 만 비교하지 않도록

답변

4
, 당신의 = == 또는 ===로 변경

.

+0

근무 완벽하게 감사를보십시오. 몇 분 후에 대답을 받아 들일 것입니다. – jckly

+1

또는'==='를 비교하여 * 비교 *를 변환하지 않았는지 확인하십시오. – James

+0

@James 네 말이 맞아. 일반적으로 더 좋다. 나는 갱신했다. –

0
if ($("#body").hasClass("questionone")){ 
    var $currentQuestion = 1; 
    console.log($currentQuestion); 
    } 
    else if ($("#body").hasClass("questiontwo")){ 
    var $currentQuestion = 2; 
    console.log($currentQuestion); 
    } 
    else if ($("#body").hasClass("questionthree")){ 
    var $currentQuestion = 3; 
    console.log($currentQuestion); 
    }; 

$(".nextquestion").on("click", function(){ 
    if($currentQuestion == 1) { 
     window.location.href = "question2.html"; 
    } 
    else if($currentQuestion == 2) { 
     console.log("thisworks"); 
     window.location.href = "question3.html"; 
    } 
    else if($currentQuestion == 3) { 
     window.location.href = "question4.html"; 
    }; 
    }); 

관련 문제