angularjs move focus to next control on enter

后端 未结 10 906
南旧
南旧 2020-11-30 07:07

What is the best way, when hitting enter inside a form, the focus to go to the next input instead submitting the form with angularjs.

I have a form with a lot of fi

10条回答
  •  执念已碎
    2020-11-30 07:30

    I tried this solution out. As advertised, it needed some tweaking. Here is what ended up working for me:

    .directive("focus", function () {
            return {
                restrict: "A",
                link: function ($scope, elem, attrs) {
                    var focusables = $(":focusable");
                    elem.bind("keydown", function (e) {
                        var code = e.keyCode || e.which;
                        if (code === 13) {
                            var current = focusables.index(this);
                            var next = focusables.eq(current + 1).length ? focusables.eq(current + 1) : focusables.eq(0);
                            next.focus();
                            e.preventDefault();
                        }
                    });
                }
            }
    

    Note that the in order to get the :focusable pseudo to work, you will need to reference JQueryUI. (the latest version 1.11.4 worked for me)

提交回复
热议问题