0

定义为添加数字输入的指令

    app.directive('numericCheck', function () {
    return {
        link: function (scope, element, attr) {
            element.on('keypress', function (event) {  
                event = (event) ? event : window.event;
                if(/Firefox/.test(window.navigator.userAgent) && event.keyCode!=0) {
                    return true;
                }
                var charCode = (event.which) ? event.which : event.keyCode;
                if (charCode > 31 && charCode != 46 && charCode != 47 && charCode != 45 && (charCode < 48 || charCode > 57) || (charCode==45 && (event.target.selectionStart!=0 || event.target.value.indexOf('-')>=0)) || (charCode==46 && event.target.value.indexOf('.')>=0) || (charCode==47 && (event.target.selectionStart==0 || event.target.value.indexOf('/')>=0))) {
                    return false;
                }
                return true;
            });  
        }

    };
});

下面的字符串是添加到 div 的响应。

{"question":"Two identical charts have different parts shaded. Which one is greater - 0.02 or 0.04?<br><br><br><br><input type='text' id='b1' name='blank_1' class='num_blank' size='5' numeric-check> is the greater number."}

将响应添加到 HTML 数字检查后,指令不起作用,它允许我们输入所有字符而不仅仅是数字值。

4

1 回答 1

0

您是否尝试过放置类似的内容:

app.directive('numericCheck', function () {
return {
    link: function (scope, element, attr) {
        element.on('keypress', function (event) { 
scope.$apply(function(){ 
            event = (event) ? event : window.event;
            if(/Firefox/.test(window.navigator.userAgent) && event.keyCode!=0) {
                return true;
            }
            var charCode = (event.which) ? event.which : event.keyCode;
            if (charCode > 31 && charCode != 46 && charCode != 47 && charCode != 45 && (charCode < 48 || charCode > 57) || (charCode==45 && (event.target.selectionStart!=0 || event.target.value.indexOf('-')>=0)) || (charCode==46 && event.target.value.indexOf('.')>=0) || (charCode==47 && (event.target.selectionStart==0 || event.target.value.indexOf('/')>=0))) {
                return false;
            }
            return true;
});
        });  
    }

};

于 2017-01-21T10:38:20.887 回答