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

常见的有:手机号,电话,验证码,序列号

<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>

 开发过程中用写的一个脚本,记录下来以备后用与他用,其中attributes["max"].nodeValue是取HTML自定义的 max属性(兼容Firefox和IE)

<html>
<head>
<meta http-equiv="content-Type" content="text/html;charset=utf-8">
<title>输入CD-KEY自动分割</title>
<script language="JavaScript" type="text/javascript">

function cdkeyInput(thisobj)
{
	objResult = "";
	nextObjResult = "";

	if(thisobj.value.indexOf('-') > 0)
	{
		objResult = thisobj.value.substring(0,thisobj.value.indexOf('-'));
		nextObjResult = thisobj.value.substring(thisobj.value.indexOf('-')+1);

		thisobj.value = objResult.substring(0,thisobj.attributes["max"].nodeValue);
		if(arguments[1] != undefined && arguments[1] != null)
		{
			next = arguments[1];
			nextobj = document.getElementById(next);
			nextobj.value = nextObjResult.toUpperCase();
			nextobj.onkeyup();
		}
	}
	thisobj.value = thisobj.value.toUpperCase();
}

</script>
</head>
<body>
</br>
<input type="text" name="key1" id="key1" max="8" size="8" onkeyup="cdkeyInput(this,'key2');"/>-
<input type="text" name="key2" id="key2" max="4" size="4" onkeyup="cdkeyInput(this,'key3');"/>-
<input type="text" name="key3" id="key3" max="4" size="4" onkeyup="cdkeyInput(this,'key4');"/>-
<input type="text" name="key4" id="key4" max="4" size="4" onkeyup="cdkeyInput(this,'key5');"/>-
<input type="text" name="key5" id="key5" max="4" size="4" onkeyup="cdkeyInput(this,'key6');"/>-
<input type="text" name="key6" id="key6" max="8" size="8" onkeyup="cdkeyInput(this);"/>
</body>
</html>

 

你可能感兴趣的:(JavaScript,html,脚本)