angularjs move focus to next control on enter

后端 未结 10 909
南旧
南旧 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:38

    Pure JavaScript Enter as TAB

    angular.module('app').directive('tabNext', function () {
    return {
        restrict: 'A',
        link: function (scope, elem) {
    
            elem.bind('keyup', function (e) {
                var code = e.keyCode || e.which;
                if (code === 13) {
                    e.preventDefault();
                    var eIDX = -1;
                    for (var i = 0; i < this.form.elements.length; i++) {
                        if (elem.eq(this.form.elements[i])) {
                             eIDX = i;
                             break;
                        }
                    }
                    if (eIDX === -1) {
                        return;
                    }
                    var j = eIDX + 1;
                    var theform = this.form;
                    while (j !== eIDX) {
                        if (j >= theform.elements.length){
                            j = 0;
                        }
                        if ((theform.elements[j].type !== "hidden") && (theform.elements[j].type !== "file")
                                && (theform.elements[j].name !== theform.elements[eIDX].name) 
                                && (! theform.elements[j].disabled) 
                                && (theform.elements[j].tabIndex >= 0)) {
                            if (theform.elements[j].type === "select-one") {
                                theform.elements[j].focus();
                            } else if (theform.elements[j].type === "button") {
                                theform.elements[j].focus();
                            } else {
                                theform.elements[j].focus();
                                theform.elements[j].select();
                            }
                            return;
                            break;
                        }
                        j++;
                    }
                }
            });
        }
    }});
    

提交回复
热议问题