[英]AngularJs directive for only num does not work on IE11
我正在使用 angularjs 指令,該指令應確保輸入字段只接受數字。 該指令適用於 Chrome 和 FF,但不適用於 IE11
需要添加什么才能使其工作? 或者指令在 IE11 中不起作用? 我不確定,但如果有人能夠幫助我找出解決方法,或者如果我在代碼中犯了錯誤,那就太好了。
我的版本是 11.0.9600.19596 - 更新版本:11.0.170。 在這個版本中,我可以在框中輸入字母字符,這與它在 Chrome 和 FF 中的行為相反。
我也在 IE11 Version 11.1392.17763.0 Update Version 11.0.205 上試過
不知道還有什么要檢查的。
謝謝,
埃拉斯莫
AngularJS for onlynum
app.directive('onlyNum', function () {
return function (scope, element, attrs) {
var keyCode = [8, 9, 37, 39, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 110];
element.bind("keydown", function (event) {
console.log($.inArray(event.which, keyCode));
if ($.inArray(event.which, keyCode) == -1) {
scope.$apply(function () {
scope.$eval(attrs.onlyNum);
event.preventDefault();
});
event.preventDefault();
}
});
};
});
HTML:
<div class="w-25 p-3">
<label for="meeting-id">Meeting ID</label>
<input type="number" ng-model="meetingId" id="meeting-id" ng-trim="true" class="form-control" placeholder="Enter Meeting ID" only-num />
</div>
我使用以下代碼示例,它可以在 IE 11 和其他瀏覽器中正常運行:
<!DOCTYPE html>
<html ng-app="test">
<head>
<meta charset="utf-8" />
<title></title>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
</head>
<body ng-controller="MainCtrl">
<div class="w-25 p-3">
<label for="meeting-id">Meeting ID</label>
<input type="number" ng-model="meetingId" id="meeting-id" ng-trim="true" class="form-control" placeholder="Enter Meeting ID" only-num />
</div>
<script>
var app = angular.module('test', []);
app.controller('MainCtrl', function ($scope) {
$scope.name = 'World';
});
app.directive('onlyNum', function () {
return {
restrict: 'A',
link: function (scope, elm, attrs, ctrl) {
elm.on('keydown', function (event) {
if (event.shiftKey) { event.preventDefault(); return false; }
if ([8, 13, 27, 37, 38, 39, 40].indexOf(event.which) > -1) {
// backspace, enter, escape, arrows
return true;
} else if (event.which >= 49 && event.which <= 57) {
// numbers
return true;
} else if (event.which >= 96 && event.which <= 105) {
// numpad number
return true;
}
else {
event.preventDefault();
return false;
}
});
}
}
});
</script>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.