Issue
Specifically, I do NOT want to use the already-recommended method of event.preventDefault()
when the enter key is pressed, because that breaks functionality for datalists in at least Internet Explorer.
I've tried something along the lines of
$("#form").submit(function(event){
console.log(event);
return false; // Allows me to read the console before the form submits!
});
To compare the differences between the events (clicking the submit button vs hitting the enter key), and there doesn't seem to be anything obvious. I would have expected the latter to have an event.keyCode
, but that's not the case.
A long-winded workaround that I can think of seems to be to remove my submit button, and replace it with a regular button whose click is bound to submitting the form. But that way, I'll have to capture the enter key presses in inputs and deal with submitting the form from scratch. Ugly, but doable. Again, I do NOT want to apply any preventDefault()
s if I can help it.
Solution
Kind of a hacky solution, but you can tie the event handler to a click
on the submit
button, instead of a submit
of the #form
. Then you can check if there were non-zero coordinates associated with the event. If so, the submit
button was clicked, if not, the enter key was pressed. e.g.
$("input[type='submit']").click(function(event){
if (event.screenX === 0) alert("You hit the enter key!");
else alert("You clicked the submit button!");
return false; // Allows me to read the console before the form submits!
});
Answered By - bowheart
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.