html部分代码:
<td align="right" width="15%">
<label class="Validform_label">
<font color="red">*</font>身份证号:
<td class="value" width="35%">
<input id="sfz" name="sfz" type="text" maxlength="100" style="width:65%" class="inputxt" datatype="idCard"/>
<span class="Validform_checktip">
<label class="Validform_label" style="display:none;">身份证号
<td align="right" width="15%">
<label class="Validform_label">
<font color="red">*</font>性别:
<td class="value" width="35%">
<t:dictSelect field="xb" type="radio" typeGroupCode="sex" defaultVal="${ttmCooperationTeacherPage.xb}" extendJson="{datatype:'*'}"></t:dictSelect>
<span class="Validform_checktip">
<label class="Validform_label" style="display:none;">性别
<td align="right" width="15%">
<label class="Validform_label">
<font color="red">*</font>出生年月:
<td class="value" width="35%">
<input id="csrq" name="csrq" type="text" maxlength="36" style="width:150px" class="Wdate" onClick="WdatePicker({dateFmt:'yyyy-MM'})" datatype="*" />
<span class="Validform_checktip">
<label class="Validform_label" style="display:none;">出生年月
<td align="right" width="15%">
<label class="Validform_label">
<font color="red">*</font>年龄:
<td class="value" width="35%">
<input id="age" name="age" type="text" maxlength="36" style="width:65%" class="inputxt" datatype="n" />
<span class="Validform_checktip">
<label class="Validform_label" style="display:none;">年龄
</tr>
js部分代码:
$("#sfz").bind("input propertychange",function () {//监控输入框的变化
var str =$("#sfz").val();//获取输入框的值
if(str.length==18){
debugger;
var birthday = str.substr(str.length-12,6);
//字符串转换位日期格式yyyy-MM
var date_str = birthday.replace(/(\d{4})(\d{2})/g,'$1-$2');
//将值传入文本框
document.getElementById("csrq").value = date_str;
//获取当前年
var myDate =new Date();
//截取身份证上的年份
var year= str.substr(str.length-12,4);
//计算年龄
var age = myDate.getFullYear() - year;
//将值传入文本框
document.getElementById('age').value = age;
//获得性别
var sexValue = str.substr(16,1);
if (sexValue%2==0) {
//女
$('input:radio').eq(1).attr('checked', 'true');
}else {
//男
$('input:radio').eq(0).attr('checked', 'true');
}
}
});