2
I have a input text
within a form
, however I do not want the form
is keyed ENTER. The code below works, but if I decode the alert
the form
is submitted even having the two lines below. Can anyone explain to me why?
$(document).ready(function() {
$('#txtPesquisaServidor').keydown(function(event){
if(event.keyCode == 13) {
//alert('enter!');
event.preventDefault();
return false;
}
});
});
I’m using jQuery 1.6.2
EDIT 1
The alert
is only illustrative, while typing ENTER the input
will have the same behavior as button click https://jsfiddle.net/o0mkjnwk/
EDIT 2
I got it! If I were to use alert
would have to set the timeout
or use the stop()
so that the code did not get confused, thus sending the form
(what was not the desired). The problem, in fact, does not exist without the alert
. I just put my original code to test and it worked smoothly.
Code working: https://jsfiddle.net/o0mkjnwk/1/
I thank everyone who contributed.
Which browser are you testing on? Having or not
alert();
should not make a difference in behavior. You can make a jsFiddle that shows the behavior?– Sergio
They suggested to use a newer version of the plugin and replace the keydown with . on('keypress'). Follow https://jsfiddle.net/ysj6uk54/
– feresjorge