使用javascript选择整个输入值时,文本框值应该会更改

时间:2013-03-07 07:00:06

标签: javascript jsp

我有两个文本框,如果第一个文本框达到maxlength,它会自动聚焦到第二个文本框 现在问题是再次输入maxlength后我选择在第一个文本框中输入的整个值并尝试编辑不同的值,但它会移动到第二个文本框。

<!DOCTYPE html>

<html>
    <body>
        <script>
        function branchAutoTab(){

            current = document.getElementById('primaryFilterBranchTextBox1');

            next = document.getElementById('primaryFilterBranchTextBox2');

            if (current.getAttribute&&current.value.length==current.getAttribute("maxlength")){

                next.focus();
            }
        }
        </script>

        <input id="primaryFilterBranchTextBox1" type="text" maxlength="3" class="controlFormatExtraSmall" onkeypress="javascript:if (event.keyCode!=8)branchAutoTab();" >
        &nbsp;&nbsp;Producer&nbsp;
        <input id="primaryFilterBranchTextBox2" type="text" maxlength="6" class="controlFormatExtraSmall"/> 
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

在代码片段中使用onkeydown而不是onkeypress:

<input id="primaryFilterBranchTextBox1" type="text" maxlength="3" class="controlFormatExtraSmall" onkeypress="javascript:if (event.keyCode!=8)branchAutoTab();" > &nbsp;&nbsp;Producer&nbsp;
          <input id="primaryFilterBranchTextBox2" type="text" maxlength="6" class="controlFormatExtraSmall"/>  

使用此:

<input id="primaryFilterBranchTextBox1" type="text" maxlength="3" class="controlFormatExtraSmall" onkeydown="javascript:if (event.keyCode!=8)branchAutoTab();" > &nbsp;&nbsp;Producer&nbsp;
          <input id="primaryFilterBranchTextBox2" type="text" maxlength="6" class="controlFormatExtraSmall"/>  

修改逻辑如下:

if(current.getAttribute&amp;&amp; current.value.length&gt; current.getAttribute(“maxlength”)){                  current.value = current.value.substr(0,current.value.length);                 next.focus();             }

检查这是否有帮助