现在的位置: 首页 > 综合 > 正文

达到文本框的最大长度时跳到下一个文本框

2013年10月30日 ⁄ 综合 ⁄ 共 956字 ⁄ 字号 评论关闭

 

<html>

<head>

<script type="text/javascript">

function checkLen(x,y)

{

if (y.length==x.maxLength)

{

var next=x.tabIndex

if (next<document.getElementById("myForm").length)

   {

   document.getElementById("myForm").elements[next].focus()

   }

}

}

</script>

</head>

 

<body>

<p>这段脚本在达到文本框的最大长度时跳到下一个文本框:</p>

 

<form id="myForm">

<input size="3" tabindex="1" maxlength="3" onkeyup="checkLen(this,this.value)">

<input size="2" tabindex="2" maxlength="2" onkeyup="checkLen(this,this.value)">

<input size="3" tabindex="3" maxlength="3" onkeyup="checkLen(this,this.value)">

</form>

</body>

 

 

又或者用以下方法

 

 

 

 

<html>

<head>

<script type="text/javascript">

function checkLen(obj,id)

{

 if (obj.value.length==obj.maxLength)

 {

   document.getElementById(id).focus()

  }

}

</script>

</head>

 

<body>

<p>这段脚本在达到文本框的最大长度时跳到下一个文本框:</p>

 

 

<input id="5" size="3" tabindex="1" maxlength="3" onkeyup="checkLen(this,'6')">

<input id="6" size="2" tabindex="2" maxlength="2" onkeyup="checkLen(this,'7')">

<input id="7" size="3" tabindex="3" maxlength="3" >

 

</body>

 

抱歉!评论已关闭.