change focus to the next input text with angularjs

后端 未结 2 646
执念已碎
执念已碎 2021-01-12 14:51

I have an object variable in my controller (var myObject), divided into 3 input text in the IHM.

I want to change automatically the focus to the next input when the

2条回答
  •  粉色の甜心
    2021-01-12 15:09

    You'd need to use a directive for this:

    app.directive("moveNextOnMaxlength", function() {
        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();
                        }
                    }
                });
            }
        }
    });
    

    And update your form as follows:

    Demo

    You could move the directive onto the

    element instead, but the build-int jqLite's find() method will restrict you to only finding elements by tag name. If you're using full jQuery, or can use vanillaJS instead, I would suggest this method.

提交回复
热议问题