项目里要实现一个input验证通过就切换到下一个input的功能
当然用jq dom操作很简单 ,大家都懂,现在用 angular,mvc 数据模型控制分离,不想再dom操作怎么办
以下方法
<textarea style="height: 73px;min-height: 73px;resize: none "
type="text"
focus-me="item.Bake.FocusNext"
maxlength="500"
ng-model="item.Bake.PromoText" />
app.directive('focusDir',[ "$timeout","$parse" ,function($timeout,$parse) {
return {
link: function($scope, element, attrs) {
var model = $parse(attrs.focusDir);
$scope.$watch(model, function(value) {
// console.log('value=',value);
if(value === true) {
$timeout(function() {
element[0].focus();
});
}
});
// to address @blesh's comment, set attribute value to 'false'
// on blur event:
element.bind('blur', function() {
// console.log('blur');
$scope.$apply(model.assign($scope, false));
});
}
};
}] );
//Tip:记得 引入"$timeout","$parse"
这个事focus ,类似的 大家可以用于kedown keyup等等
引用:http://stackoverflow.com/questions/14833326/how-to-set-focus-on-input-field