admin管理员组文章数量:1419192
$("#username,#password").keypress(function(e)
{
//alert('');
if(e.keyCode == 13)
{
signIn();
}
});
The keypress event not calling if the enter button is pressed.
$("#username,#password").keypress(function(e)
{
//alert('');
if(e.keyCode == 13)
{
signIn();
}
});
The keypress event not calling if the enter button is pressed.
Share Improve this question edited Oct 22, 2012 at 12:10 GordonM 31.8k17 gold badges93 silver badges132 bronze badges asked Oct 22, 2012 at 12:09 NiDZNiDZ 932 silver badges7 bronze badges 2-
1
do the selected elements exist when code is run? If not use
on()
to delegate handler. If they exist..is code wrapped indocument.ready
– charlietfl Commented Oct 22, 2012 at 12:13 - Any specific browser? Seems to be working here – user1726343 Commented Oct 22, 2012 at 12:13
5 Answers
Reset to default 4Try using keyUp event.
Live Demo
$("#username,#password").keyup(function(e){
//alert('');
if(e.keyCode == 13)
{
signIn();
}
});
This is also working with keypress
Live Demo
$("#txt1").keypress(function(e) {
if (e.keyCode == 13) {
//signIn();
alert("keypress");
}
});
Key down is more appropriate:
$("#username,#password").keydown(function(e){
if(e.keyCode == 13)
{
signIn();
}
});
- Use
e.which
instead ofe.keyCode
because this is what jQuery guarantees to be on event - Try
e.preventDefault()
, because without it (if they are in a form) the form submits - Maybe the best would be listening on form's
submit
event
Are you sure it's not the signIn ();
function that's faulty? Because this seems to work just fine; http://jsfiddle/vDkBs/1/
Use keypress and which statement:
$("#username,#password").keypress(function(e) {
if (e.which == 13) {
//call here your function
}
});
本文标签: javascriptJquery key press event not triggered on enterStack Overflow
版权声明:本文标题:javascript - Jquery key press event not triggered on enter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745282713a2651518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论