HTML5中jQuery实现身份证自动赋值生日和性别
页面
<div class="col-xs-3 lr-form-item" data-table="lr_base_user"> <div class="lr-form-item-title">生日</div> <input id="F_Birthday" type="text" class="form-control lr-input-wdatepicker" onfocus="WdatePicker({ dateFmt:'yyyy-MM-dd',onpicked: function () { $('#F_Birthday').trigger('change'); } })" /> </div> <div class="col-xs-3 lr-form-item" data-table="lr_base_user"> <div class="lr-form-item-title">性别<font face="宋体">*</font></div> <div id="F_Gender" isvalid="yes" checkexpession="NotNull"></div> </div>
js处理function中方法里:
//身份证号改变时发生 $('#F_IDCard').on("input propertychange", function () { var card = $(this).val(); if (card.length > 15) { //性别和出生日期 var birth = card.substring(6, 10) + "-" + card.substring(10, 12) + "-" + card.substring(12, 14); var sex = ""; if (parseInt(card.substr(16, 1)) % 2 == 1) { sex = "1"; } else { sex = "0"; } $('#F_Birthday').val(birth); $('#F_Gender').lrselectSet(sex); //获取年龄 var myDate = new Date(); var month = myDate.getMonth() + 1; var day = myDate.getDate(); var age = myDate.getFullYear() - ele.substring(6, 10) - 1; if (ele.substring(10, 12) < month || ele.substring(10, 12) == month && ele.substring(12, 14) <= day) { age++; } } });