2013-03-22 1 views
0

정규 표현식으로 Charector를 모니터하고 싶습니다. 요컨대e.keycode는 firefox 19.0.2, chrome 25.0.1364.172 m, IE 8에서는 정의되지 않았습니다.

$('#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!! 
+0

케이스, 여기 MHM 민감 : 기본적으로 in source ishttp://api.jquery.com/event.which/을. – jolt

+0

'e.which'가 jQuery에서 표준화되었으므로 대부분의 사람들이 이것을 사용합니다! – adeneo

답변

7

, 자바 스크립트는 대소 문자를 구분한다.

e.keycode != e.keyCode.

그리고, 당신은 camelCase 사용해야은 '일 거라고 - $ 기호 jQuery를 가정하면 e.keyCode

을, 나는 코드를 찾을 때이 e.which을 사용하는 것이 좋습니다 것입니다. 모든 브라우저와 호환되도록 정규화되었습니다.

는 여기를 참조하십시오 :

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

당신이 downvote 경우, 감사합니다 이유와 함께 코멘트를 제공하십시오. – jolt

관련 문제