javascript - Can't make "Enter" key call a function instead of submit page -
This code works fine when I map the function to "Ctrl" key (character code 17). But when I change it to "ant" key (code 13), this page submits why this happens?
& lt; Html & gt; & Lt; Top & gt; & Lt; Script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" src = "../ utilities / utility.js" & gt; & Lt; / Script & gt; & Lt; Script & gt; $ (Document) .ready (function () {$ ("# userInput"). Key (action) {if (event.keyCode == 17} {// <-> <--- ***** here Correct ***** Process Input ();}}); Flashcard_man ();}); & Lt; / Script & gt; & Lt; Script type = "text / javascript" src = "flashcards.js" & gt; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; P id = "question" & gt; Question paragraph & Lt; / P & gt; & Lt; P id = "sub_question" & gt; Sub-question & lt; / P & gt; & Lt; Form name = "chloroform" & gt; & Lt; Input id = "user input" type = "text" & gt; & Lt; Input id = "userSubmitBtn" type = "button" value = "submit" onclick = "processInput ()" & gt; & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt; Edit:
I changed the function below but I get the same problem:
$ (document). Ready (function () ($ ("# user input"). Key (action) {if (event.keyCode == 13} {event.preventDefault (); processInput ();}}); flashcard_man (); };
You can type preventDefault () To stop the default behavior of pressed keys: You must use keypress event:
$ ("# us ErInput ") keypress (function (event) {if (event.keyCode == 17 | | Event.keyCode == 13) {event.preventDefault (); processInput ();}});
Comments
Post a Comment