我正在尝试编写一个指令,只允许数字和一个十进制值。但当按下数字键时,移位键会显示特殊字符。
请建议一种不使用正则表达式的方法,因为我不想让用户键入其他字符
setTargetPage.directive('numPercent', function () {
return {
restrict: 'A',
link: function (scope, element, attr, ctrl) {
var keyCode = [8, 9, 37, 39, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 110, 190];
element.bind("keydown", function (event) {
if ($.inArray(event.which, keyCode) == -1) {
event.preventDefault();
}
else {
if ((event.which == 110 || event.which == 190) && element.val().indexOf('.') > -1) {
event.preventDefault();
}
}
});
}
}
});
当按下Shift时,有一个event.shiftKey(纯JS对AngularJS没有特别的需要)为true。