如何在javascript中提取特殊键?

时间:2013-03-22 11:14:56

标签: javascript jquery html regex

我正在调用搜索文本框的.keyup功能,在keyup()中,我正在刷新数据库中的GRID。

问题:

但网格正在刷新(特殊键也是)箭头键,数字锁定,功能键和所有其他键,并且不需要刷新这些键。退格,退货,制表符,空格,删除。

我想构造一个正则表达式,以便过滤掉所有控制键。

示例代码:

$('#searchContent').keyup(function (e) {
    var key = e.which;
    if ( /*condition*/ ) {
        return;
    }
    //my code goes here...
}

我做了什么:

彻底搜索网络,我想出了hotkey,但这并没有解决我的目的。那么,有没有明智的正则表达式?

2 个答案:

答案 0 :(得分:0)

尝试使用此HTML查看哪些代码与哪些代码匹配(取自Mozilla):

<html>
<head>
    <title>charCode example</title>

    <script type="text/javascript">

        function showChar(e)
        {
            alert("Key Pressed: " + String.fromCharCode(e.charCode) + "\n"
                  + "charCode: " + e.charCode);
        }

        </script>
    </head>

    <body onkeypress="showChar(event);">
        <p>Press any 'character' type key.</p>
    </body>
</html>

答案 1 :(得分:0)

尝试这个技巧:

function checkForChanges(){
   var txt = $('#searchContent');
   if(txt.val() != txt.attr('xvalue') {
      //do referesh grid content here
   }
}

$('#searchContent').keyup(function (e) {
    $(this).attr('xvalue') = this.value;    
    clearTimeout(this.changeTimer);    
    this.changeTimer = setTimeout(checkForChanges, 20);
}