2013-06-08 1 views
0

나는 다음 코드를 가지고 있는데, 나는 JavaScript에 비해 상대적으로 새로운 편이다. 그래서 아무도 내가 1 단계로 나아 가지 않고 왜 그것이 완료되었는지 보여주지 못한다고 말할 수 있을까?jQuery의 발전 단계

$(function(){ 
    var textContainer = $('#text'), 
     inputLine = $('input'); 

    textContainer.text('What is your name?'); 
    inputLine.focus().on('keyup', function(e){ 
     if (e.which === 13) { 
      e.preventDefault(); 
      textContainer.text('Hi there, ' + this.value + '.'); 
      this.value = ""; 
     } 
    }); 
}); 

FIDDLE

이 방법 stage 없다 : 당신은 많은 의미가하지 않는, 그래서이 추측하고있어 무엇

var textContainer = '#text'; 
var inputLine = 'input'; 

var username = null; 
var stage = 0; 

$(function(){ 
     if(0 == stage){ 
      $(function(){ 
       $(textContainer).text('What is your name?'); 
       $(inputLine).focus(); 
       $(inputLine).keypress(function(e){ 
        if (e.keyCode == 13 && !e.shiftKey) { 
         e.preventDefault(); 
         username = $(this).val(); 
         $(this).val(''); 
         stage = 1; 
        } 
       }); 
      }); 
     } 
     if(1 == stage){ 
      $(textContainer).text('Hi there, ' + username + '.'); 
     } 
    }); 
+0

이 함수를 호출하는 코드는 어디에 있습니까? –

+1

'$ (function() {'은'$ (document) .ready()'의 줄임말입니다.) – Blender

+0

다음 if 문으로 넘어 가고 싶었습니다 (if (1 == stage)) – JazzyP

답변

1

당신이 뭘 하려는지입니다 0으로 설정 한 후 바로 0이 아닌 다른 값이 될 수 있습니까?
이벤트 처리기 내부에서 어떤 일이 발생하는지 "나중에"발생하므로 이벤트 처리기 후에 stage을 확인하면 여전히 대기 중입니다. 기다리시겠습니까?

관련 문제