Change focus to input on a key event in AngularJS

后端 未结 3 1578
有刺的猬
有刺的猬 2021-02-03 11:49

Test case: http://jsbin.com/ahugeg/4/edit (Slightly long)

In the above test case, I have three input elements, generated by ng-repeat directive. My intentio

3条回答
  •  [愿得一人]
    2021-02-03 12:26

    Inspired by @Trevor's solution, here's what I settled on,

    app.directive('focusIter', function () {
    
        return function (scope, elem, attrs) {
            var atomSelector = attrs.focusIter;
    
            elem.on('keyup', atomSelector, function (e) {
                var atoms = elem.find(atomSelector),
                    toAtom = null;
    
                for (var i = atoms.length - 1; i >= 0; i--) {
                    if (atoms[i] === e.target) {
                        if (e.keyCode === 38) {
                            toAtom = atoms[i - 1];
                        } else if (e.keyCode === 40) {
                            toAtom = atoms[i + 1];
                        }
                        break;
                    }
                }
    
                if (toAtom) toAtom.focus();
    
            });
    
            elem.on('keydown', atomSelector, function (e) {
                if (e.keyCode === 38 || e.keyCode === 40)
                    e.preventDefault();
            });
    
        };
    });
    

    This defines an attribute focus-iter to be set on the parent element of all the repeated inputs. See this in action here: http://jsbin.com/ahugeg/10/.

    The advantage over @Trevor's is that I can set an arbitrary selector for the value of focus-iter attribute to specify exactly which elements the focus jumping should work with. As a crazy example, try setting focus-iter attribute to input:even :). This helps since in my application, the inputs come with quite a bit of extra markup around them, unlike the test case.

提交回复
热议问题