将焦点移至上一个输入指令

时间:2018-10-16 09:00:10

标签: javascript angularjs input

我当前有一条指令,当用户键入一个值时,该指令从一个输入移到下一个输入。我的输入只允许输入1位数字。看起来如下:

_ _ _

,填写后,将类似于1 1 1

我的指令现在如下:

(function () {
    "use strict";



 angular
        .module("movenext.input", [])
        .directive("moveNextInput", moveNextInput);

    moveNextInput.$inject = [];

    function moveNextInput() {
        return {
            restrict: 'A',
            link: function ($scope, element) {
                element.on("input", function (e) {
                    if (element.val().length == element.attr("maxlength")) {
                        var $nextElement = element.next();
                        if ($nextElement.length) {
                            $nextElement[0].focus();
                        }
                    }
                });
            }
        };
    }

我的html:

 <input class="mb20" type="tel" id="new_auth1" move-next-input move-previous-input  ng-click="newCodeAutorizationfocus(1)" maxlength="1">

和点击功能

$scope.newCodeAutorizationfocus = function (n) {
    $timeout(function () {
        var id = '#new_auth' + n;
        $(id).focus();
    }, 500);
};

我的问题是标题中所说的与此相反。 我尝试了以下功能,但没有成功:

function movePreviousInput() {
        return {
            restrict: 'A',
            link: function ($scope, element) {
                element.on("input", function (e) {
                    if (element.val().length == element.attr("maxlength")) {

                        var $previousElement = element;
                        console.log($previousElement);
                        if ($previousElement.length) {
                            $previousElement[$previousElement.length - 1].focus();
                        }

                    }
                });
            }
        };
    }

0 个答案:

没有答案
相关问题