【问题标题】:How to scroll to the first invalid input in angular forms? [closed]如何滚动到角度形式的第一个无效输入? [关闭]
【发布时间】:2019-06-19 05:43:55
【问题描述】:

我正在从事一个大型的有角项目。如何在不使用 jquery 的情况下滚动到角度形式的第一个无效输入?

【问题讨论】:

标签: javascript jquery angularjs


【解决方案1】:

你可以这样做:-

myApp.directive('accessibleForm', function () {
return {
    restrict: 'A',
    link: function (scope, elem) {

        // set up event handler on the form element
        elem.on('submit', function () {

            // find the first invalid element
            var firstInvalid = elem[0].querySelector('.ng-invalid');

            // if we find one, set focus
            if (firstInvalid) {
                firstInvalid.focus();
            }
        });
      }
   };
});

【讨论】:

  • 这段代码应该写在哪个文件里?
  • 你可以在上面的指令中使用它。
猜你喜欢
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多