How to prevent input key input in jQuery?

I would just like to learn the keystroke of the Enter key in the <input> field or instead replace the keystrokes. I have not decided which is better.

How to do it in jQuery? I still have this:

 $(document).ready(function(){ ... //handle enter key $("input").keypress(function (e) { var k = e.keyCode || e.which; if (k == 13) { //??? } }); }); 

(This part of e.keyCode || e.which been recommended in this matter .)

What can I do there to (a) cancel the event, or (b) make the Tab key press?

+6
jquery submit html-form keypress
source share
3 answers

With e.preventDefault (), you can prevent the default action, which in this case submits the form.

+14
source share

Or return false; :

 $(document).ready(function(){ ... //handle enter key $("input").keypress(function (e) { var k = e.keyCode || e.which; if (k == 13) { return false; // !!! } }); }); 
+7
source share

Alternatively, if you just want to simulate a tab for the next control, as you mentioned, you can try this jdsharp method .

 $(document).ready(function(){ ... //handle enter key $("input").keypress(function (e) { var k = e.keyCode || e.which; if (k == 13) { $(this).focusNextInputField(); } }); }); 
+2
source share

All Articles