Spring mvc +jquery ajax+json的异步实例

本文介绍了一个使用身份证号码自动填充表单字段的示例,包括前端JavaScript代码和后端Java处理逻辑。当用户输入身份证号码后,系统会自动填充姓名、性别、出生日期等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端部分代码

$(document).ready(function() {
// 根据身份证号填充表单
$("#p_Bip_bip_citizenid").blur(function() {
if (isIdCardNo($("#p_Bip_bip_citizenid").val())) {
$.ajax({
type: "POST",
url: "/gljt.allowance.do",
data: "m=loadSubtractBipById&idCard=" + $("#p_Bip_bip_citizenid").val(),
dataType: "json",
success: function(jsonData) {
if (jsonData.result=='none') {
alert("您所录入的身份证不是高龄津贴的新增人员,无法录入减少人员数据");
} else if(jsonData.result=='error'){
alert("提取信息失败,请稍后在试。");
document.getElementById("p_Bip_bip_name").readOnly = false;
//没有查到信息则根据身份证将将性别、生日字段读出并赋值
} else if(jsonData.result=='empty'){
document.getElementById("p_Bip_bip_name").readOnly = false;
$("input[name='p_Bip_bip_sex']").each(function(i,data){if($(data).val()==idcard_getsex($("#p_Bip_bip_citizenid").val())){$(data).attr("checked",true);}});
if(idcard_getsex($("#p_Bip_bip_citizenid").val())=="1"){
setFormValue('p_Bip_','sex12','男');
setFormValue('p_Bip_','bip_sex','1');
}
if(idcard_getsex($("#p_Bip_bip_citizenid").val())=="2"){
setFormValue('p_Bip_','sex12','女');
setFormValue('p_Bip_','bip_sex','2');
}
$("input[name='p_Bip_bip_birthday']").val(idcard_getbirthday($("#p_Bip_bip_citizenid").val()));
} else{
document.getElementById("p_Bip_bip_name").readOnly = true;
//循环JSON数据 自动匹配元素赋值
for (var one in jsonData) {
for(var key in jsonData[one]) {
var level = document.getElementById("level").value;
var street_id = document.getElementById("streetId").value;
if(level == 2) {
if (key != "unit_id") {
setFormValue('p_Bip_',key,jsonData[one][key]);
} else {
if(key == "unit_id"){
if(street_id != jsonData[one]["community_parentId"]){
setFormValue('p_Bip_','unit_id','');
alert("您所录入的[老年人口]不属于本街道,如需录入请继续!");
} else {
setFormValue('p_Bip_','unit_id',jsonData[one][key]);
}
}
}
}else if(level == 3){
if (key != "unit_id") {
setFormValue('p_Bip_',key,jsonData[one][key]);
} else {
if(key == "unit_id") {
if(street_id != jsonData[one]["unit_id"]){
setFormValue('p_Bip_','unit_id','');
alert("您所录入的[老年人口]不属于本社区,如需录入请继续!");
} else {
setFormValue('p_Bip_','unit_id',jsonData[one][key]);
}
}
}
} else {
setFormValue('p_Bip_',key,jsonData[one][key]);
}
if(key=="bip_sex"){
if(jsonData[one][key]=="1"){
setFormValue('p_Bip_','sex12','男');
}
if(jsonData[one][key]=="2"){
setFormValue('p_Bip_','sex12','女');
}
}
//if(key=="lsphoto"){
//PreviewImagejs('Preview',jsonData[one][key],133,187);
//}
if(key=="lsphoto"){
if(jsonData[one][key]!=null && jsonData[one][key] != ""){
document.getElementById("pho").style.display = "";
document.getElementById("Preview").style.display = "none";
//PreviewImagejs('Preview',jsonData[one][key],133,187);
var url="/servlet/ImagesServlet?path="+jsonData[one][key];
var photo=document.getElementById("idImg");
//photo.setAttribute("src",url);
photo.src = url;
}
else{
document.getElementById("pho").style.display = "none";
document.getElementById("Preview").style.display = "";

}
}
}
}
}
}
});
}
});
});

后端部分代码:

public void loadBipByIdCard(HttpServletRequest request,
HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
PrintWriter pw = null;
try {
pw = response.getWriter();
Bip bip = new Bip();
String bip_citizenid = ServletRequestUtils.getStringParameter(
request, "idCard", "");
bip.setBip_citizenid(bip_citizenid);
List<Bip> bips = this.bipService.queryByVO(bip);

bip = bips.size() < 1 ? null : bips.get(0);
if (null == bip) {
pw.println("{\"result\":\"empty\"}");
} else {
if(bip.getUnit_id() != null && bip.getUnit_id() != ""){
bip.setStreet_id(MirrorCacheTool.getProp("unit", bip.getUnit_id(), "parentid").toString());
}
JSONArray json = JSONArray.fromObject(bip, JSONDataFormat
.setDataFormat());
pw.println(json);
}

} catch (Exception e) {
pw.println("{\"result\":\"error\"}");
e.printStackTrace();
}
pw.flush();
pw.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值