2017-03-05 2 views
2

나는를 keyDownjQuery를 사용하여 백 스페이스를 트리거하는 방법은 무엇입니까?

$('#input-div').keydown(function(e) { 
if(e.which == 13) { 
    userInput = $(this).val(); 
    $(this).val(userInput.substring(0,0)); 

이 입력 상자를 지 웁니다에 다양한 이벤트를 트리거 아래의 코드가 있습니다. 그러나 엔터 키를 누르면 커서가 두 번째 줄로 이동합니다. Enter 키를 놓은 후에 커서가 원래 위치로 되돌아 가기를 원합니다. 키 위로.

어떤 도움이 필요합니까?

답변

1

간단히 e.preventDefault() 추가

$('#input-div').keydown(function(e) { 
 
    if(e.which == 13) { 
 
    userInput = $(this).val(); 
 
    $(this).val(userInput.substring(0,0)) 
 
    e.preventDefault() 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<textarea id="input-div"></textarea>

+0

e.preventDefault에 초점을 .focus()를 사용할 수있다() 간단하게 문제를 해결 감사. – Neo

0
<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <script 
    src="https://code.jquery.com/jquery-3.1.1.min.js" 
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" 
    crossorigin="anonymous"></script> 
</head> 
<body> 
    <form method="POST" id="userform"> 
     <input type="text" name="firstname" id="firstname"> 
     <input type="text" name="lastname" id="lastname"> 
    </form> 

    <script type="text/javascript"> 
    $("#userform").submit(function(e){ 
     return false;  
    }); 

    $("#userform input").keydown(function(e) { 
     if(e.which == 13) { 
      userInput = $(this).val(); 
      $(this).val(userInput.substring(0,0)); 
      $("#userform input#firstname").focus(); 
     }  
    });  
</script> 
</body> 
</html> 

는 특정 입력 상자

관련 문제