接受的答案是有效的,但前提是這些字段是直接的兄弟姐妹。例如,如果你有每3個領域中有自己的專欄,你需要一個不同的解決方案:
angular
.module('move-next-directive', [])
.directive('moveNextOnMaxlength',
function() {
return {
restrict: "A",
link: function(scope, elem, attrs) {
elem.on('input', function(e) {
var partsId = attrs.id.match(/focus(\d+)/);
var currentId = parseInt(partsId[1]);
var l = elem.val().length;
if (l == elem.attr("maxlength")) {
nextElement = document.querySelector('#focus' + (currentId + 1));
nextElement.focus();
}
});
}
}
}
);
添加帶編號的「焦點」的ID給每個輸入字段:
<div class="row">
<div class="col-xs-4">
<input type="text" id="focus1" maxlength="4" move-next-on-maxlength />
</div>
<div class="col-xs-4">
<input type="text" id="focus2" maxlength="4" move-next-on-maxlength />
</div>
<div class="col-xs-4">
<input type="text" id="focus3" maxlength="4" />
</div>
</div>
貸這樣的回答: https://stackoverflow.com/a/33007493/3319392
也許,以下可能會有所幫助:http://stackoverflow.com/questions/18086865/angularjs-move-focus-to-next-control-on-enter – 2016-04-30 22:03:51