简体   繁体   English

如何在输入类型时间内设置自动对焦

[英]How set auto focus next in input type time

I have a simple input with type of time and I try to set auto focus next input in the DOM tree 我有一个简单的time类型输入,尝试在DOM树中设置自动聚焦下一个输入

<input type="time" name="startTime" (keyup)="setFocus($event)"/>

setFocus(element) {
    if (element.target.value.length == 5) {

        let coll = document.getElementsByTagName('input');
        let array = Array.from(coll)
        let thisOne = array.forEach((el) => {
            if (el == element.target) {
                let index = array.indexOf(el)

                let next = array[index + 1]

                next.focus();
                next.select();
            }
        });


    }
}

The problem is when the time had default value the value length is always 5 (on keyup) like 05:45 or 18:22 , so how could I set next auto focus? 问题是当时间具有默认值时,值的长度始终为5(在键入时),如05:4518:22 ,那么如何设置下一个自动聚焦? Is there any way to find out the cursor is in the 5th position? 有什么办法可以找出光标在第5位吗? any idea? 任何想法?

Knowing that you have id for the input element, then you can set focus with JavaScript focus method like this 知道您有输入元素的ID后,就可以使用JavaScript焦点方法设置焦点,如下所示

document.getElementById(next.id).focus();

Edit: I found that focus doesn't work if element don't have id, if you set your element id, then your code will perfectly work. 编辑:我发现如果元素没有ID,则焦点将不起作用,如果您设置了元素ID,则您的代码将可以正常工作。

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

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