根据tabindex,光标如何自动跳转到下一个可能的(未禁用的)输入字段?

I got that function (source: 100 ) but my cursor doesn't move automatically after input of 1 value: - Why?

function focusNextElement () {
//add all elements we want to include in our selection
var focussableElements = 'a:not([disabled]), button:not([disabled]), input[type=text]:not([disabled]), [tabindex]:not([disabled]):not([tabindex="-1"])';
if (document.activeElement && document.activeElement.form) {
    var focussable = Array.prototype.filter.call(document.activeElement.form.querySelectorAll(focussableElements),
    function (element) {
        //check for visibility while always include the current activeElement 
        return element.offsetWidth > 0 || element.offsetHeight > 0 || element === document.activeElement
    });
    var index = focussable.indexOf(document.activeElement);
    if(index > -1) {
       var nextElement = focussable[index + 1] || focussable[0];
       nextElement.focus();
    }                    
}
}

我的输入字段的示例:

<table>
<thead></thead>
<tbody>
<tr>
<td> 1<input type="text" maxlength="1" oninput="focusNextElement(this)" id="a" name="a" tabeindex="1">.   </td>
 <td>3<input type="text" maxlength="1" oninput="focusNextElement(this)" id="b" name="b" tabeindex="3" disabled></td>
 <td>5<input type="text" maxlength="1" oninput="focusNextElement(this)" id="c" name="c" tabeindex="5"></td></tr>
 <tr>
 <td>2 <input type="text" maxlength="1" oninput="focusNextElement(this)" id="d" name="d" tabeindex="2"></td>
 <td>4<input type="text" maxlength="1" oninput="focusNextElement(this)" id="e" name="e" tabeindex="4"></td>
 <td>6<input type="text" maxlength="1" oninput="focusNextElement(this)" id="f" name="f"  tabeindex="6""></td>
 </tr>
 </tbody>
 </table>

示例:https://jsfiddle.net/rydjnzkw/4/

推荐答案

您不需要执行这段代码的大部分内容,只需querySelector个未禁用的输入,try 查找此数组中的当前元素索引,然后查找focus个下一个元素.

const inputs = document.querySelectorAll('input:not([disabled])')

function focusNextElement (input) {
  const nextIndex = [...inputs].findIndex(i => i === input) + 1
  if (inputs[nextIndex]) {
    inputs[nextIndex].focus()
  }
}
<input type="text" maxlength="1" oninput="focusNextElement(this)" id="a" name="a">
<input type="text" maxlength="1" id="c" name="b" disabled>
<input type="text" maxlength="1" oninput="focusNextElement(this)" id="d" name="c">

UPDATE

让它按照tabeindex来工作

const inputs = document.querySelectorAll('input:not([disabled])');
let focusableElement;
let maxTries = 100;
let counter = 0;
const getNextElement = (tabeindex) => {
  const element = document.querySelector(`[tabeindex="${tabeindex + 1}"]`)
  if (counter >= maxTries) {
    return;
  }
  if (element && !element.disabled) {
    counter = 0;
    focusableElement = element;
    return;
  }
  counter++
  getNextElement(tabeindex + 1)
}

function focusNextElement (input) {
  const nextIndex = [...inputs].findIndex(i => i === input) + 1
  const tabeindex= +input.getAttribute('tabeindex');
  const element = getNextElement(tabeindex)
  focusableElement.focus()
}
<table>
<thead></thead>
<tbody>
<tr>
 <td> 1<input type="text" maxlength="1" oninput="focusNextElement(this)" id="a" name="a" tabeindex="1"></td>
 <td>3<input type="text" maxlength="1" oninput="focusNextElement(this)" id="b" name="b" tabeindex="3" disabled></td>
 <td>5<input type="text" maxlength="1" oninput="focusNextElement(this)" id="c" name="c" tabeindex="5">
</td></tr>
<tr>
 <td>2 <input type="text" maxlength="1" oninput="focusNextElement(this)" id="d" name="d" tabeindex="2"></td>
 <td>4<input type="text" maxlength="1" oninput="focusNextElement(this)" id="e" name="e" tabeindex="4"></td>
 <td>6<input type="text" maxlength="1" oninput="focusNextElement(this)" id="f" name="f"  tabeindex="6""></td>
</tr>
</tbody>
</table>

Javascript相关问答推荐

自定义帖子类型帖子未显示在网站上

在JavaScript中,是否有一种方法可以创建自定义thable,在等待某些代码后自动触发它?

在分区内迭代分区

使用TMS Web Core中的HTML模板中的参数调用过程

如何在使用fast-xml-parser构建ML时包括属性值?

如何最好地从TypScript中的enum获取值

如何使用侧边滚动按钮具体滚动每4个格?

使用axios.获取实时服务器时的404响应

没有输出到带有chrome.Devtools扩展的控制台

配置WebAssembly/Emscripten本地生成问题

CheckBox作为Vue3中的一个组件

如何在 cypress 中使用静态嵌套循环

Angular 中的类型错误上不存在获取属性

从包含数百行的表中获取更改后的值(以表单形式发送到后端)的正确方法是什么?

如何在文本字段中输入变量?

如何将数据块添加到d3力有向图中?

搜索功能不是在分页的每一页上进行搜索

在D3条形图中对具有相同X值的多条记录进行分组

将嵌套数组的元素乘以其深度,输出一个和

如何使pdf.js上的文本呈现为可选?