按Enter键时不会触发按键事件 [英] keypress event is not fired when we press enter key

查看:95
本文介绍了按Enter键时不会触发按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家我正面临关于按键事件的问题。当我按下回车键时,按键事件不会被触发,但它与其他键一起工作正常。

Hi Guys i am facing a problem regarding to the keypress event. When i press the enter key then keypress event is not fired but it is working fine with the other keys.

这是我的代码:

$(document).ready(function () {
        alert('hi');
        $("#ctl00_popupPageBody_txtFirstName,#ctl00_popupPageBody_txtLastName").keypress(function (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            alert(code);
            if (code == 13) { //Enter keycode
                //Do something
            }
        });
    });


推荐答案

你应该使用 keyup 此事件

$(document).ready(function() {
    $("#ctl00_popupPageBody_txtFirstName,#ctl00_popupPageBody_txtLastName").keyup(function(e) {
        if (e.which == 13) {
            //Enter keycode //Do something 
        }
    });
});

这篇关于按Enter键时不会触发按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆