It is necessary to make a small console, send entered by the user should by clicking on the enter. There is such a code

$("#consoleForm").keypress(function(event){//<input type="text"> if(event.keyCode === 13){ //Остальной код } }); 

But when you click on the enter page reloads. How to stop reboot? PS This <input type = "text"> remembers what was entered into it and throws out a hint how to remove it?

  • 1. return false; 2. autocomplete="off" - Igor

1 answer 1

Get rid of the keypress handler and use submit :

 var $dest = $("pre"), $src = $("input[type='text']"); $("form").submit(function (event) { event.preventDefault(); $dest.text($dest.text() + "\n" + $src.val()); $src.val(""); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form> <pre></pre> <input type=text autofocus><input type=submit value=Ok> </form> 

  • Reboot anyway. Moreover, only the reaction to Enter is needed, no buttons. - Dima Lukashov
  • @ DimaLukashov, no. - Qwertiy
  • @DimaLukashov, i.e. when you press enter in my example, the line is not added? After all, it is added. - Qwertiy