javascript 使用 angularjs 将焦点更改为下一个输入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28449622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change focus to the next input text with angularjs
提问by Karim Oukara
I have an object variable in my controller (var myObject), divided into 3 input text in the IHM.
我的控制器中有一个对象变量 (var myObject),在 IHM 中分为 3 个输入文本。
I want to change automatically the focus to the next input when the focused one reached the maxLength.
当焦点达到 maxLength 时,我想将焦点自动更改为下一个输入。
var myObject = {
part1:"",
part2:"",
part3:""
}
<form>
<input type="text" id="part1" ng-model="myObject.part1" maxlength="7"/>
<input type="text" id="part2" ng-model="myObject.part2" maxlength="12"/>
<input type="text" id="part2" ng-model="myObject.part2" maxlength="12"/>
</form>
回答by CodingIntrigue
You'd need to use a directive for this:
您需要为此使用指令:
app.directive("moveNextOnMaxlength", function() {
return {
restrict: "A",
link: function($scope, element) {
element.on("input", function(e) {
if(element.val().length == element.attr("maxlength")) {
var $nextElement = element.next();
if($nextElement.length) {
$nextElement[0].focus();
}
}
});
}
}
});
And update your form as follows:
并按如下方式更新您的表单:
<form>
<input type="text" id="part1" ng-model="myObject.part1" maxlength="7" move-next-on-maxlength />
<input type="text" id="part2" ng-model="myObject.part2" maxlength="12" move-next-on-maxlength />
<input type="text" id="part2" ng-model="myObject.part2" maxlength="12"/>
</form>
You couldmove the directive onto the <form>
element instead, but the build-int jqLite's find()
method will restrict you to only finding elements by tag name. If you're using full jQuery, or can use vanillaJS instead, I would suggest this method.
您可以将指令移到<form>
元素上,但 build-int jqLite 的find()
方法将限制您只能通过标签名称查找元素。如果您使用完整的 jQuery,或者可以使用 vanillaJS,我会建议使用此方法。
回答by Jette
Accepted answer works, but only if the fields are immediate siblings. If for example you have 3 fields each in there own column, you need a different solution:
接受的答案有效,但前提是这些字段是直系兄弟姐妹。例如,如果您有 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();
}
});
}
}
}
);
Add a numbered "focus" id to each input field:
为每个输入字段添加一个编号的“焦点”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>
Credits to this answer: https://stackoverflow.com/a/33007493/3319392