2013-03-22 60 views
2

我打電話.keyup搜索文本框的功能,並在那keyup(),我刷新數據庫中的GRID。如何在JavaScript中提取特殊鍵?

問題:

但網格是越來越刷新的(特殊鍵也行)方向鍵,數字鍵鎖定功能鍵和其他按鍵和清爽的這些鍵是不必要的。除了退格,退回,製表符,空格,刪除。

我想構造一個正則表達式,以便過濾出所有的控制鍵。

示例代碼:

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

我做了什麼:

搜索網徹底,我想出了hotkey,但這並不解決我的目的。那麼,任何聰明的正則表達式在那裏?

+3

也許你應該使用'onchange'事件? – RJo 2013-03-22 11:16:52

+3

@RJo'onchange'只會在模糊afaik上觸發。 – 2013-03-22 11:24:57

+0

@Rjo現在只有我注意到傑克說的話。 – 2013-03-22 11:27:14

回答

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> 
+0

或使用jquery [LINK](http://api.jquery.com/keyup/)的此鏈接... :) – 2013-03-22 11:36:24

+2

但是,當前在此答案中使用的'keypress'事件可能不會觸發刪除或退格。 .. – nnnnnn 2013-03-22 11:36:31

+0

它將在chrome和id incase of delete或backspace中失敗 – 2013-03-22 11:58:30

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); 
} 
相關問題