JavaScript Keycode 46是DEL功能键还是(。)句号? [英] JavaScript Keycode 46 is DEL Function key or (.) period sign?

查看:81
本文介绍了JavaScript Keycode 46是DEL功能键还是(。)句号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im writing some logic in JavaScript using jquery, where i must check the input content against a REGEX pattern ex:

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

逻辑几乎完成,我只是有一个问题,过滤功能键DEL,
我的逻辑是这样的:

The logic is almost done, i just have a little problem filtering the function key DEL, my logic goes like this:

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。

If the KeyCode is one of those in the array, i let it pass, if not i get the char and compare it against the REGEX. The problem is: in some Browsers the DEL and '.' (period sign) have the same key Code 46.

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

So is there a better logic to filter the function keys or must i write a condition for that case, maybe removing the 46 from the array and try to convert it to char and if is (.) let it go to the Regex function if not let it pass? The other question will be are there more shared Key Codes in some browsers?

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

My suggested solution wont work because it doesn't matter which key the user pressed (DEL or period) i always get (.) as CHAR at least on OPERA and FF =(.

推荐答案

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

110 is the decimal key code, 46 is the DEL key.

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

For some fun: put this in to see what you hit! added a focused event

   /* 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));
  });
});

这篇关于JavaScript Keycode 46是DEL功能键还是(。)句号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆