JavaScript Keycode 46是DEL功能键还是(。)句号?

时间:2010-05-18 17:34:49

标签: javascript jquery keyboard keypress

我使用jquery在JavaScript中编写一些逻辑,其中我必须根据REGEX模式检查输入内容:

"^[a-zA-Z0-9_]*$"  //Alpha-numeric and _

逻辑差不多完成,我只是在过滤功能键DEL时遇到一些问题, 我的逻辑是这样的:

var FunctionsKey = new Array(8, 9, 13, 16, 35, 36, 37, 39, 46);

function keypressValidation(key) {
            if (config.regexExp != null) {
                if ($.inArray(key, FunctionsKey) != -1) {
                    return true;
                }
                else {
                    var keyChar = String.fromCharCode(key);
                    return RegexCheck(keyChar);
                }
            }
            return true;
        }

如果KeyCode是数组中的其中一个,我让它通过,如果没有,我得到char并将它与REGEX进行比较。 问题是:在一些浏览器中DEL和'。' (期间标志)具有相同的密码46。

有没有一个更好的逻辑来过滤功能键或者我必须为该情况写一个条件,可能从数组中删除46并尝试将其转换为char,如果是(。)让它转到正则表达式功能,如果不让它通过? 另一个问题是在某些浏览器中是否有更多的共享密钥代码?

编辑:我建议的解决方案不起作用,因为用户按下哪个键(DEL或句点)并不重要我总是得到(。)作为CHAR,至少在OPERA和FF =(。

5 个答案:

答案 0 :(得分:10)

110是十进制密钥代码,46是DEL密钥。

为了一些有趣的事情:把它放进去看看你打了什么!编辑:添加了一个重点事件

   /* handle special key press */
$(document).ready(function() {
  function checkAKey(e) {
    var shouldBubble = true;
    switch (e.keyCode) {
      // user pressed the Tab 
      case 9:
        {
          alert("Tab hit, no bubble");
          shouldBubble = false;
          break;
        };
        // user pressed the Enter    
      case 13:
        {
          alert("Enter");
          break;
        };
        // user pressed the ESC
      case 27:
        {
          alert("Escape");
          break;
        };
    };
    /* this propogates the jQuery event if true */
    return shouldBubble;
  };

  $("*").keydown(function(e) {
    return checkAKey(e);

  });
});

OR

$(document).ready(function() {
  /* handle special key press */
  function checkFieldKey(e, me) {
    var shouldBubble = true;
    switch (e.keyCode) {
      // user pressed the Enter
      case 13:
        {
          $(me).blur();
          $("#somewhereElse").focus();
          shouldBubble = false;
          break;
        };
    };
    /* this propogates the jQuery event if true */
    return shouldBubble;
  };
  /* user pressed special keys while in Selector */
  $("#myField").keydown(function(e) {
    return checkFieldKey(e, $(this));
  });
});

答案 1 :(得分:6)

键盘上的十进制或点键代码为190 ..小数位数为110。

欢呼声.. !!

答案 2 :(得分:3)

@Mark Schultheiss'的回答非常好,我还要补充一些:当你需要在输入元素之外的键盘上按 DEL 按钮时(即当文本字段失去焦点时)你必须拦截它。 这可以这样做:

$("#selector-for-a-textbox, body").keydown(function(event){
  if(event.keyCode==46){
     // do something here
  }
});

答案 3 :(得分:3)

区别在于: 在按键" DELETE"键和"。" key返回键码46。

所以在keyup或key down上编写代码,然后" DELETE"关键效果很好。

检查http://javascript.info/tutorial/keyboard-events

错误的代码

 window.addEventListener("keypress", dealWithKeyboard, false);

正确的代码:

window.addEventListener("keydown", dealWithKeyboard, false);

最终代码

 window.addEventListener("keydown", dealWithKeyboard, false);
 function dealWithKeyboard(e) {   // CHECK KEYPRESS EVENT
         if(e.keyCode ==46){
           delete_object();} 
      }

对我来说效果很好。

答案 4 :(得分:0)

如果我们想要小数点前的n位数字和小数点后的n位数字,我们可以简单地使用,

var validate = function(e) {
var beforedecimal=5;
var afterDecimal=4;
  var t = e.value;
  var n = t.includes(".");
  if(n== true){
    e.maxLength = 15;
  e.value = (t.indexOf(".") >= 0) ? (t.substr(0, t.indexOf(".")) + t.substr(t.indexOf("."), afterDecimal)) : t;
}else if(n == false){
    var a=t.length;
    e.maxLength = beforedecimal;
    if(a>=beforedecimal){
        if(event.keyCode == 46){
            e.maxLength = beforedecimal+1;
        }
    }
    
}

}
<input type="text" id="resultText" onkeypress="validate(this)" />