【发布时间】:2016-07-20 16:02:55
【问题描述】:
我实现了一个自动添加空格的功能:
+33 6 XX XX XX XX
或
06 XX XX XX XX
这是脚本:
$('#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() + " ");
}
}
});
它在 chrome 上完美运行,但在 Firefox 上我无法退格输入。有什么想法吗?
这是一个 jsfiddle 来说明:
【问题讨论】:
-
请注意,从用户体验的角度来看,它在 Chrome 中也不起作用。一个例子 - 输入一个数字,意识到你已经调换了两位数。选择它们并尝试以正确的方式重写它们 - 随之而来的是奇怪。
-
哦,对了,没试过这个......这很奇怪我做错了什么?
标签: javascript jquery keypress onkeypress