繁体   English   中英

仅用于 num 的 AngularJs 指令在 IE11 上不起作用

[英]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.

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