简体   繁体   English

使用angular js自动滚动到第一个空白必填字段

[英]autoscroll to first blank required field using angular js

I have created a form using angular js with around 7 input elements. 我已使用带有约7个输入元素的angular js创建了一个表单。 When I click on submit, I want the form to get scrolled up to the first blank field which is required. 当我单击提交时,我希望表格滚动到所需的第一个空白字段。 But now it is not correctly pointing to the field left blank. 但是现在它不能正确指向空白的字段。 Any solution to resolve this ? 有解决方案吗?

Check the error here. 在此处检查错误。

before submitting the form, you can check whether the form is valid or not and use .focus() to focus on that element. 在提交表单之前,您可以检查表单是否有效,并使用.focus()专注于该元素。

$scope.onSubmit = function(yourForm) {
  if (!yourForm.$valid) {
    angular.element("[name='" + yourForm.$name + "']").find('.ng-invalid:visible:first').focus();
    return false;
  }
};

method #2 - You can also use $anchorScroll service 方法2-您也可以使用$ anchorScroll服务

see the documentation here 这里查看文档

$scope.onSubmit = function(yourForm) {
  if (!yourForm.$valid) {
    var id = angular.element("[name='" + yourForm.$name + "']").find('.ng-invalid:visible:first').data('id');
    $location.hash(id);
    $anchorScroll();
    return false;
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM