javascript - e.keycode 在 firefox 19.0.2 、chrome 25.0.1364.172 m、IE 8 中未定义

标签 javascript jquery html browser keycode

我希望通过正则表达式来监视该字符。

$('#searchContent').keyup(function(e){

        e = e || window.event;

        var patt =/\w/g;
        var key = String.fromCharCode(e.keyCode);


        console.log ( "key " + key + e.keycode+ " is pressed!!");

        if( e.keycode != 8 && e.keyCode != 46){ //Will return if printable char is not typed. But the datagrid will still refresh on  pressing backspace.
            console.log ( "key " + key+ e.keycode + "is about to take test!!" );
            if(!patt.test(key)){ 
                console.log ( "key " + key+e.keycode + "is failed!!");
                return;
            }
            console.log ( "key " + key +e.keycode+ "is pressed passes the test!!");
        }
        else{
            console.log ( "backspace or delete has ByPasses the conditoin!!");
        }
          //  other operations....
}
    //Result of my log. INPUT : RIS(<-backspace)
    key R undefined is pressed!! index_tab.php:173
    key R undefined is about to take test!! index_tab.php:176
    key R undefined is pressed passes the test!! index_tab.php:181
    key I undefined is pressed!! index_tab.php:173
    key I undefined is about to take test!! index_tab.php:176
    key I undefined is pressed passes the test!! index_tab.php:181
    key S undefined is pressed!! index_tab.php:173
    key S undefined is about to take test!! index_tab.php:176
    key S undefined is pressed passes the test!! index_tab.php:181
    key  undefined is pressed!! index_tab.php:173 //here backspace was pressed
    key  undefined is about to take test!! index_tab.php:176
    key  undefined is failed!! 

最佳答案

简而言之,JavaScript 区分大小写。

e.keycode != e.keyCode.

并且,您应该使用 camelCase 'd 一个 - e.keyCode

假设$符号是jQuery,我建议您在查找代码时使用e.which。它已经标准化,可以与每个浏览器兼容。

请参见此处:http://api.jquery.com/event.which/ ,基本上 in source is :

// Add which for key events
if ( event.which == null ) {
    event.which = original.charCode != null ? original.charCode : original.keyCode;
}

关于javascript - e.keycode 在 firefox 19.0.2 、chrome 25.0.1364.172 m、IE 8 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15566339/

相关文章:

javascript - React Native 中的链接库故障排除

javascript - 单击按钮/链接转到评论表单

javascript - 如何使用 Jquery 将值附加到嵌套 div

html - 如何在 tweeter bootstrap 中内联显示有序列表

php - wp 作业管理器自定义过滤器

javascript - 语法错误 : Unexpected token '/' while compiling ejs but works if I remove <% include ./src/myTemplate.ejs%>

javascript - 基于复选框选择jquery更新文本框

javascript - 如何在动态生成的 HTML 表中停止递归

javascript - 如何动态地将CSS媒体类型添加到页面

javascript - 单击 AngularJS 中的链接后,信息不会刷新。我有 {{ value }} 但没有数据