简体   繁体   English

在keypress jquery函数上,退格不起作用

[英]on keypress jquery function, backspace doesn't work

I implemented a function that add white space like so automatically : 我实现了一个自动添加空白区域的函数:

+33 6 XX XX XX XX

or 要么

06 XX XX XX XX

Here is the script : 这是脚本:

$('#num').keypress(function(){
    $this = $(this);
    //remove whitespaces to calculate the length of the string
    $val = $this.val().replace(/\s+/g, '');
    //if it's the case '+33 6 XX XX XX XX
    if ($val.toLowerCase().indexOf("+") >= 0 | $val == "")
    {
     if($val.length == 3){
            $this.val($this.val() + " ");
        }else if($val.length == 4){
            $this.val($this.val() + " ");
        }else if ($val.length >= 5 && ($val.length)%2 == 0){
            $this.val($this.val() + " ");
        }
    }else{
         if (($val.length)%2 == 0){
            $this.val($this.val() + " ");
        }
    }

});

It works perfectly on chrome but on firefox i can't backspace the input.. Any ideas ? 它在chrome上完美运行但在firefox上我无法退回输入..任何想法?

Here is a jsfiddle to illustrate : 这是一个jsfiddle来说明:

https://jsfiddle.net/jd1mwp3p/ https://jsfiddle.net/jd1mwp3p/

You need to filter non-printable keypress events, in case the browser is firing them. 如果浏览器触发它们,您需要过滤不可打印的按键事件。

For instance, for backspace: 例如,对于退格:

$('#num').keypress(function(e) {
  if(e.keyCode == 8) {
    return true;
  }
  $this = $(this);
  // etc.
}

Use the .keydown and you do not have to worry about checking whether backspace or not! 使用.keydown ,你不必担心检查是否退格! because it will work with any key pressed. 因为它可以使用任何按下的键。

There are 2 things to think about here. 这里有两件事要考虑。

The first one is that different browsers handle keypress,keyup, keydown, differently. 第一个是不同的浏览器以不同的方式处理keypress,keyup,keydown。 In particular when it comes to non printable chars, such as backspace, some browsers fire the keypress event and some don't. 特别是当涉及到不可打印的字符(例如退格键)时,某些浏览器会触发按键事件,而某些浏览器则不会。 Firefox does, and Chrome doesn't. Firefox确实如此,而Chrome则没有。

Now if you look at your code, you don't take in consideration backspace. 现在,如果您查看代码,则不会考虑退格。 Indeed what you do is adding a space if there is no + sign and the value is even: 实际上,如果没有+符号且值为偶数,则执行的操作是添加空格:

else{
         if (($val.length)%2 == 0){
            $this.val($this.val() + " ");
        }
    }

and you again add a space if the value is 3 4 or 5 chars long when there is a + or the value is empty: 当有一个+或值为空时,如果值为3 4或5个字符长,则再次添加一个空格:

if ($val.toLowerCase().indexOf("+") >= 0 | $val == "")
    {
     if($val.length == 3){
            $this.val($this.val() + " ");
        }else if($val.length == 4){
            $this.val($this.val() + " ");
        }else if ($val.length >= 5 && ($val.length)%2 == 0){
            $this.val($this.val() + " ");
        }
    }

So. 所以。 Chrome ignores your keypressed event and just deletes the chars, firefox calls your event and you do not handle backspace. Chrome会忽略您的按键事件,只删除字符,firefox调用您的事件,而您不会处理退格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM