复选框tabindex意外行为

时间:2013-06-12 20:22:22

标签: javascript html tabindex

我正在使用此javascript自动跳转到下一个字段:

    <script>
    function checkLen(x,y)
    {
    if (y.length==x.maxLength)
      {
      var next=x.tabIndex;
      if (next<document.getElementById("formid").length)
        {
        document.getElementById("formid").elements[next].focus();
        }
      }
    }
    </script>

这是我的表格:

<form action="random.php" id="formid" method="post">
   <input type="checkbox" name="cbox" tabindex="4" />Default
   <input type="text" name="text1" id="text1" tabindex="1" />
   <input type="text" name="text2" id="text2" tabindex="2" maxlength="1" onkeyup="checkLen(this,this.value)" />
   <input type="text" name="text3" id="text3" tabindex="3" />
</form>

当我在text2中输入单个字符(maxlength="1")时,会跳转到tabindex为4的复选框,而逻辑上,它是指跳转到text3 tabindex of 3。

我已经完成了各种各样的事情,例如从复选框中删除tabindex,给它最后一个tabindex命令,但它不起作用。

我希望我的插入符号在键入单个字符时跳转,从text2到text3。

1 个答案:

答案 0 :(得分:1)

由于输入的顺序,tabindex的{​​{1}}实际上对应于text2集合中从零开始的序数位置(2),导致您关注elements 1}}。您需要在text2中添加1才能在next集合中选择text3

相关问题