2017-03-05 6 views
2

私は、これは、入力ボックスをクリアKeyDownイベントjQueryでスペースを戻す方法は?

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

に様々なイベントをトリガー以下のコードを持っています。しかし、エンターキーが押されているので、カーソルは2行目に移動します。 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()は単に、問題を解決しましたありがとう。 – 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> 

あなたは、特定の入力ボックスに集中する.focusを()を使用することができ

関連する問題