2017-10-11 76 views
0

我用这个angularjs指令来限制用户只按数字。而且绝对有效。但是,当我使用删除键来删除输入字段数据。它不适用于我。因此,任何人都有一个想法,允许删除键来删除输入字段数据。AngularJs指令:如何使用删除键删除输入字段数据

请帮我搞定这个。任何帮助将不胜感激。

在此先感谢。

app.directive('allowOnlyNumbers', function() { 
return { 
    restrict: 'A', 
    link: function (scope, elm, attrs, ctrl) { 
     elm.on('keydown', function (event) { 
      if (event.which == 64 || event.which == 16) { 
       // to allow numbers 
       return false; 
      } else if (event.which >= 48 && event.which <= 57) { 
       // to allow numbers 
       return true; 
      } else if (event.which >= 96 && event.which <= 105) { 
       // to allow numpad number 
       return true; 
      } else if ([8, 13, 27, 37, 38, 39, 40].indexOf(event.which) > -1) { 
       // to allow backspace, enter, escape, arrows 
       return true; 
      } else { 
       event.preventDefault(); 
       // to stop others 
       return false; 
      } 
     }); 
    } 
} 

});

+1

删除键的键码是'46' – pbibergal

回答

0

如果将其中一部分添加到您的指令中,请添加此部分。

else if (event.which == 46) { 
    // to allow delete 
    return true; 
} 

希望它能工作。