当达到文本域的最大字符数时跳至下一个域

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function checkLen(x,y)
  5. {
  6. if (y.length==x.maxLength)
  7. {
  8. var next=x.tabIndex
  9. if (next<document.getElementById("myForm").length)
  10. {
  11. document.getElementById("myForm").elements[next].focus()
  12. }
  13. }
  14. }
  15. </script>
  16. </head>
  17.  
  18. <body>
  19. <p>这段脚本在达到文本框的最大长度时跳到下一个文本框:</p>
  20.  
  21. <form id="myForm">
  22. <input size="3" tabindex="1" maxlength="3" onkeyup="checkLen(this,this.value)">
  23. <input size="2" tabindex="2" maxlength="2" onkeyup="checkLen(this,this.value)">
  24. <input size="3" tabindex="3" maxlength="3" onkeyup="checkLen(this,this.value)">
  25. </form>
  26. </body>
  27.  
  28. </html>