http xanimes.tv:后面输入框比前面输入框自动加一年如何实现

来源:百度文库 编辑:高校问答 时间:2024/04/18 11:28:05
就是前面一个文本框填上日期之后,后面的框,自动填上下一年的日期

谢谢次教

<html>
<head>
<style TYPE="text/css">
<!--
.style1 {background-color:#ffffff;color:#ff0000;border-style:solid;border-width:1;width:240}
.style2 {background-color:#ffffff;color:#397594;border-style:solid;border-width:1;width:240}
-->
</style>
</head>
<body>
<script language="javascript">
function check(dateStr)
{
var matchArray = dateStr.match(/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/)
if (matchArray == null)
{
document.getElementById("a").value="日期格式检测中!|正确格式:YYYY-MM-DD"
document.getElementById("a").className='style1';
document.getElementById("input2").value=""
return false;
}
document.getElementById("a").value="日期格式正确!"
document.getElementById("a").className='style2';
var str,str1
str=document.getElementById("input1").value.substring(0,4)
str1=document.getElementById("input1").value.substring(4,10)
str=Number(str)+1
document.getElementById("input2").value=str+str1
return true;
}
</script>
请输入日期:<input id="input1" value="" maxlength="10" onpropertychange="check(this.value)"><input id=a class="style1"><br>
一年后日期:<input id="input2" value="" maxlength="10" readonly>
</body>
</html>

代码这样写:

<form name="form1">

<input name="date1" value="" onchange="javascript:NextYear();">
<input name="date2" value="">

<script language="javascript">

function NextYear()
{

var tmp,tmp1,tmp2,tmp3,length;

tmp=form1.date1.value;
length=tmp.length;

tmp1=tmp.substring(0,3);
tmp2=tmp.substring(3,4);
tmp3=tmp.substring(4,length);

tmp2=Number(tmp2)+1;

tmp=tmp1+tmp2+tmp3;
form1.date2.value=tmp;

}

</script>

</form>

这只是很简单的将第四个字符转换成数字然后加一,这就要求输入的必须是2005-01-01这种格式的合法日期才行。关于如何判断输入是否日期的代码请参考:

http://www.zahui.com/html/7/14370.htm