16

フォーム内でEnterキーを押すと、angularjsでフォームを送信する代わりに、次の入力にフォーカスするのが最善の方法です。

多くのフィールドを持つフォームがあり、顧客は Enter キーを押して次の入力 (デスクトップ アプリケーションから) に移動することに慣れています。ユーザーがEnterキーを押すと、angularjsはフォームを保存します。私はこれを変えるのが好きです。出来ますか ?

4

10 に答える 10

13

カスタム ディレクティブを作成することをお勧めします。このようなもの。私はこれをテストしていません。

.directive('focus', function() {
  return {
    restrict: 'A',
    link: function($scope,elem,attrs) {

      elem.bind('keydown', function(e) {
        var code = e.keyCode || e.which;
        if (code === 13) {
          e.preventDefault();
          elem.next().focus();
        }
      });
    }
  }
});

そのようなものがうまくいくはずです。何かを微調整する必要があるかもしれません。幸運を。

于 2013-08-06T17:57:28.480 に答える
10

カスタム ディレクティブを作成します。

.directive('nextOnEnter', function () {
    return {
        restrict: 'A',
        link: function ($scope, selem, attrs) {
            selem.bind('keydown', function (e) {
                var code = e.keyCode || e.which;
                if (code === 13) {
                    e.preventDefault();
                    var pageElems = document.querySelectorAll('input, select, textarea'),
                        elem = e.srcElement || e.target,
                        focusNext = false,
                        len = pageElems.length;
                    for (var i = 0; i < len; i++) {
                        var pe = pageElems[i];
                        if (focusNext) {
                            if (pe.style.display !== 'none') {
                                angular.element(pe).focus();
                                break;
                            }
                        } else if (pe === elem) {
                            focusNext = true;
                        }
                    }
                }
            });
        }
    }
})
于 2016-04-30T21:21:32.130 に答える
4

これは私が最終的に得たディレクティブです (Zack Argyle に感謝します):

    
    angular.module('myApp').directive("nextFocus", nextFocus);

    /** Usage:
      <input next-focus id="field1">
      <input next-focus id="field2">
      <input id="field3">
      Upon pressing ENTER key the directive will switch focus to
      the next field id e.g field2
      The last field should not have next-focus directive to avoid
      focusing on non-existing element.
      Works for Web, iOS (Go button) & Android (Next button) browsers, 
    **/

    function nextFocus() {
      var directive = {
        restrict: 'A',
        link: function(scope, elem, attrs) {
          elem.bind('keydown', function(e) {
            var partsId = attrs.id.match(/field(\d{1})/);
            var currentId = parseInt(partsId[1]);

            var code = e.keyCode || e.which;
            if (code === 13) {
              e.preventDefault();
              document.querySelector('#field' + (currentId + 1)).focus();
            }
          });
        }
      };
      return directive;

    }
于 2015-10-08T05:42:32.437 に答える
1

wolcy97 による回答に基づいていますが、角度のみを使用しています

 /** Usage:
  <input next-focus tabindex="0">
  <input next-focus tabindex="1">
  <input tabindex="2">
  Upon pressing ENTER key the directive will switch focus to
  the next tabindex.
  The last field should not have next-focus directive to avoid
  focusing on non-existing element.
  Works for Web, iOS (Go button) & Android (Next button) browsers, 
**/ 
 app.directive('nextFocus', [function() {
  return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
      elem.bind('keydown', function(e) {
        var code = e.keyCode || e.which;
        if (code === 13) {
          e.preventDefault();
          try {
            if (attrs.tabindex !== undefined) {
              var currentTabeIndex = attrs.tabindex;
              var nextTabIndex = parseInt(currentTabeIndex) + 1;
              var elems = document.querySelectorAll("[tabindex]");
              for (var i = 0, len = elems.length; i < len; i++) {
                var el = angular.element(elems[i]);
                var idx = parseInt(el.attr('tabindex'));
                if (idx === nextTabIndex) {
                  elems[i].focus();
                  break;
                }
              }
            }
          } catch (e) {
            console.log('Focus error: ' + e);
          }
        }
      });
    }
  };
}]);
于 2016-10-13T20:32:28.403 に答える
1

これは私が最終的に得たディレクティブです (Zack Argyle と Oleg に感謝します):

app.directive("nextFocus", 関数 () {

    /** Usage:
      <input next-focus tabindex="0" id="field1">
      <input next-focus tabindex="1" id="field2">
      <input id="field3">
      Upon pressing ENTER key the directive will switch focus to
      the next field id e.g field2
      The last field should not have next-focus directive to avoid
      focusing on non-existing element.
      Works for Web, iOS (Go button) & Android (Next button) browsers, 
    **/
    var directive = {
        restrict: 'A',
        link: function (scope, elem, attrs) {
            elem.bind('keydown', function (e) {
                var code = e.keyCode || e.which;
                if (code === 13) {
                    try {
                        if (attrs.tabindex != undefined) {
                            var currentTabIndex = attrs.tabindex;
                            var nextTabIndex = parseInt(attrs.tabindex) + 1;
                            $("[tabindex=" + nextTabIndex + "]").focus();
                        }
                    } catch (e) {

                    }
                }
            });
        }
    };
    return directive;

});
于 2015-12-29T09:10:20.560 に答える
0

Pure JavaScript 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++;
                }
            }
        });
    }
}});
于 2016-03-15T18:09:24.043 に答える