2011-04-24 7 views
3

사용자가 명령을 입력 할 때 텍스트 상자를 만들었습니다. 이 명령은 jquery-ajax를 사용하여 PHP 파일로 전달되고 서버에서 실행되며 결과가 반환됩니다. 이 결과는 div 태그를 작성하여 브라우저에 출력됩니다.jQuery의 텍스트 상자에 포커스 설정

문제는 내가 div 태그를 추가한다는 것입니다. 내가 추가하는 동안 내 텍스트 상자가 초점을 벗어난 것처럼 보입니다. 입력 한 내용을 볼 수 있도록 페이지를 스크롤해야합니다.

이것은 입력란에 입력 한 명령을받는 기능입니다.

$(function() { 
    $('#cmd').keydown(

    function(event) { 
     if (event.keyCode == 13) { 
      event.preventDefault(); /*you can call your function here*/ 
      var tmp = $(this).val(); 
      $('#cmd').val(''); 
      commands.push(tmp); 

      MyFunction(tmp); 
      /*still you can it here*/ 
     } 
    }); 
}); 

이 함수 반환 값을 수신하고, div 태그를 생성한다.

function MyFunction(msg) { 
    var cmdStr = msg; 
    $.ajax({ 
     url: 'exec.php', 
     dataType: 'text', 
     data: { 
      q: cmdStr 
     }, 
     success: function(response) { 

    $('#output').append("<div class=type> [email protected]:~# " + cmdStr +"</div>" + "<div class=output>" + response + "</div>"); 
     } 

    }); 

} 

답변

13

이 시도 :

success: function(response){ 
    $("#output").append("<div class=type> [email protected]:~# " + cmdStr +"</div>" + "<div class=output>" + response + "</div>"); 
    $("#cmd").focus(); //Wil focus your textbox 
} 
3

이 시도 :

$(function() { 
    $('#cmd').keydown(

    function(event) { 
     if (event.keyCode == 13) { 
      event.preventDefault(); /*you can call your function here*/ 
      var tmp = $(this).val(); 
      $('#cmd').val(''); 
      commands.push(tmp); 

      MyFunction(tmp); 
      $(this).focus(); // Set the focus back on to the #cmd element 
     } 
    }); 
}); 
관련 문제