Reputation: 2664
If I make submit button active (with Tab button), and then press Enter, keypress handler will fire, and will generate click event on button. But OnClick
and Submit handlers will not fire. It'll just silently post the form. Why?
<script src="../js/jquery.js" type="text/javascript"></script>
<script type="application/javascript">
$().ready(function(){
$("#f1").keypress(function(event) {
if (event.keyCode==13){
var currentInputId = $(event)[0].target.id;
var currentInput = $('#'+currentInputId);
currentInput.click();
return false;
}
});
$("#f1").submit(function(){
alert ("huy")
})
})
</script>
<form name="f1" id="f1" method="POST">
<input type="text">
<input type="submit" onclick="return false;" id="submit">
</form>
Upvotes: 0
Views: 506
Reputation: 437376
On my machine what happens in your scenario (move input on submit button, then press ENTER) is:
keypress
handler runs and calls click
on the buttononclick
returns false
If I remove the onclick="return false;"
, then:
keypress
handler runs and calls click
on the buttonsubmit
handler being calledA few more tests indicate that the submit
handler is not being called *because you return false
from the keypress
handler.
So, to solve the issue:
onclick="return false;"
from the buttonreturn false
from the keypress
handlerThis will display the alert and then submit the form as you expect it to.
Upvotes: 3