81,095
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>人名模糊查询</title>
<script type="text/javascript" src="scripts/jquery-3.2.1.js"></script>
<script type="text/javascript" src="scripts/jquery.blockUI.js"></script>
<script type="text/javascript">
function query(){
var emp = $("#in").val();
if(emp != ""){
var url = "EmployeeServlet?method=empDetailes";
var args = {"employeename": emp,"time":new Date()};
$.getJSON(url,args,function(data){
var obj = eval(data);//可正常返回数据
$("#empdetails tbody").html("");
$("#empdatails").show();
alert(obj.length); //到这里也没有问题
for (var i = 0; i < obj.length; i++) {
$("#id").text(obj[i].employeeId);
$("#name").text(obj[i].name);
$("#tel").text(obj[i].tel);
$("#phone").text(obj[i].phone);
$("#email").text(obj[i].email);
$("#address").text(obj[i].address);
}
});
}
}
</script>
</head>
<body>
<form action="">
<h1>模糊查询</h1>
请输入姓或名<input type="text" id="in"/>
<input type="submit" id="go" onclick="query()"/>
</form>
<center>
<table id="empdatails" border="1" cellspacing="0" cellpadding="5" style="display: none">
<tr>
<th>Id</th>
<th>Name</th>
<th>Tel</th>
<th>Phone</th>
<th>Email</th>
<th>Address</th>
</tr>
<tr>
<td id="id"></td>
<td id="name"></td>
<td id="tel"></td>
<td id="phone"></td>
<td id="email"></td>
<td id="address"></td>
</tr>
</table>
</center>
</body>
</html>
<tr>
<td id="id"></td>
<td id="name"></td>
<td id="tel"></td>
<td id="phone"></td>
<td id="email"></td>
<td id="address"></td>
</tr>
只有一段啊,所以最后显示的这段tr的值是你json里面最后一条数据
for (var i = 0; i < obj.length; i++) {
alert(obj[i].name);//这里可以循环取得姓名
$("#id").text(obj[i].employeeId);
$("#name").text(obj[i].name);
$("#tel").text(obj[i].tel);
$("#phone").text(obj[i].phone);
$("#email").text(obj[i].email);
$("#address").text(obj[i].address);
}