81,092
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>emp</title>
<script type="text/javascript" src="prototype-1.6.0.3.js">
</script>
<script type="text/javascript">
var xmlHttp;
//获得对象
function initRequest() {
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
//将查出的结果进行显示
function listEmpAction() {
initRequest();
var url = "http://localhost/AjaxProject/findAllEmp.jsp";
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = listEmpCallback;
xmlHttp.send(null);
}
//向查出的表中添加新员工
function insertEmpAction(){
initRequest();
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
var salary = document.getElementById("salary").value;
var url = "http://localhost/AjaxProject/insertEmp.jsp?id="+id+"&name="+name+"&salary="+salary;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = insertEmpCallback;
xmlHttp.send(null);
}
function listEmpCallback() {
clearEmpList();
if(xmlHttp.readyState == 4) {
var empList = xmlHttp.responseXML;
//appendEmp(response);
var emps = empList.getElementsByTagName("emp");
//alert(emps.length);
for(var i=0; i<emps.length; i++) {
var emp = emps.item(i);
var id = emp.getElementsByTagName("id").item(0).firstChild.text;
var name = emp.getElementsByTagName("name").item(0).firstChild.text;
var salary = emp.getElementsByTagName("salary").item(0).firstChild.text;
appendEmp(Emp(id, name, salary));
}
}
}
function insertEmpCallback() {
listEmpAction();
$("empForm");
}
function Emp(id, name, salary){
return {
"id": id,
"name": name,
"salary": salary
};
}
function appendEmp(emp){
//var info = document.getElementById("empForm");
//info.innerHTML = response;
var tr = document.createElement("tr");
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var td3 = document.createElement("td");
td1.appendChild(document.createTextNode(emp.id));
td2.appendChild(document.createTextNode(emp.name));
td3.appendChild(document.createTextNode(emp.salary));
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
$("empList").appendChild(tr);
}
function clearEmpList() {
var empList = $("empList").childNodes;
for(var i=empList.length-1; i>=0; i--) {
$("empList").removeChild(empList.item(i));
}
}
</script>
</head>
<body>
<table id="t1" border="1" width="80%">
<thead>
<tr>
<td>
ID
</td>
<td>
Name
</td>
<td>
Salary
</td>
</tr>
</thead>
<tbody id="empList">
</tbody>
</table>
<input type="button" value="List" onclick="listEmpAction();" />
<div id="empForm">
<table id="t1" border="1" width="80%">
<thead>
<tr>
<td>ID</td>
<td><input type="text" id="id"/></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" id="name"/></td>
</tr>
<tr>
<td>Salary</td>
<td><input type="text" id="salary"/></td>
</tr>
</thead>
</table>
<input type="submit" value="Insert" onclick="insertEmpAction();" />
</div>
</body>
</html>
function clearEmpList() {
var empList = $("empList").childNodes;
for(var i=empList.length-1; i>=0; i--) {
$("empList").removeChild(empList.item(i));
}
}