代码之家  ›  专栏  ›  技术社区  ›  JOBG

javascript键代码46是del函数键还是(.)句点符号?

  •  10
  • JOBG  · 技术社区  · 16 年前

    我使用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,如果是(.),如果不让它通过,就让它进入regex函数? 另一个问题是在某些浏览器中是否有更多的共享密钥代码?

    编辑:我建议的解决方案不起作用,因为不管用户按哪个键(del或period),我总是得到(.)字符,至少在opera和ff=(上是这样)。

    5 回复  |  直到 7 年前
        1
  •  9
  •   Mark Schultheiss    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);
    
      });
    });
    

    $(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));
      });
    });
    
        2
  •  6
  •   Bhaumik    12 年前

    小键盘上的十进制或点键代码是190。数字键盘上的小数是110。

    干杯。。!!

        3
  •  3
  •   vcoppolecchia    11 年前

    @马克·舒尔泰斯 答案真的很好,我再加上一点:当你需要推动 德尔 键盘上输入元素外的按钮(即当文本字段失去焦点时),您必须截取它。 可以这样做:

    $("#selector-for-a-textbox, body").keydown(function(event){
      if(event.keyCode==46){
         // do something here
      }
    });
    
        4
  •  3
  •   muTheTechie    11 年前

    区别在于: 按“删除”键,然后按“.”键返回键代码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();} 
          }
    

    它对我很管用。

        5
  •  0
  •   deepeshb    7 年前

    如果我们想要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)" />