Angular js trigger blur event

大憨熊 提交于 2020-01-03 08:14:15

问题


I'm trying to setup an input, such that when length == 5, it will automatically trigger a blur event. How can I do this?

This is pretty much the concept:

        <input type="tel" placeholder="type here." maxlength="5" name="digits" ng-model="digits" ng-change="if (digits.length ==5) { TRIGGER BLUR};">

Thanks


回答1:


Here is a basic directive that should get you what you are looking for:

app.directive('lengthChecker', function() {
  return function(scope, element, attributes) {
    scope.$watch(attributes.lengthChecker, function(newVal, oldVal) {
      if (newVal.length >= 5) element[0].blur();
    });
  };
});

Html:

<input ng-model="digits" length-checker="digits"/>


来源:https://stackoverflow.com/questions/23300329/angular-js-trigger-blur-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!