JavaScript的增删改

JavaScript的增删改

javascript的增删改,效果图如下:

代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>addUser.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="./js/wpCalendar.js"></script>
</head>

<body>
<div align="center">
<h1> 显示有的用户界面 </h1>
<div style="border: 1px red solid;margin-bottom: 100px; padding: 10px 10%">
<table border="1px" cellpadding="0" cellspacing="0" id="tusers">
<thead>
<tr>
<th> 名称 </th>
<th> 性别 </th>
<th> 邮箱 </th>
<th> 出生日期 </th>
<th> 操作 </th>
</tr>
</thead>
<tbody id="users">
</tbody>
</table>
</div>
<div style="border: 1px blue solid;">
<form id="addForm">
<table>
<tr>
<td> 用户名: </td>
<td><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td> 性别: </td>
<td><select id="sex">
<option value="男"> 男 </option>
<option value="女"> 女 </option>
</select></td>
</tr>
<tr>
<td> 邮箱: </td>
<td><input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td> 出生日期: </td>
<td><input type="text" id="bir" name="bir" />
<input type=button value="点击看我"
οnclick="showCalendar(this,document.all.bir)"></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="添加" onClick="addUser1()" />
<input type="button" value="修改" id="updateUser" /></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
<script>
window.onload = function (){
// addPage();
}

var page = null;
var users = document.getElementById("users");
var tusers = document.getElementById("tusers");

function addUser(){
var name = document.getElementById("name").value;
var sex = document.getElementById("sex").value;
var email = document.getElementById("email").value;
var bir = document.getElementById("bir").value;
//获取表格节点对象
// var tusers = document.getElementById("tusers");
//创建行
var tr1 = document.createElement("tr");
//var cbk = document.createElement("td");
var tname = document.createElement("td");
var tsex = document.createElement("td");
var temail = document.createElement("td");
var tbir = document.createElement("td");
var toper = document.createElement("td");



// cbk.appendChild(document.createTextNode("<input type='checkbox' name='cbk'/>"));
tname.appendChild(document.createTextNode(name));
tsex.appendChild(document.createTextNode(sex));
temail.appendChild(document.createTextNode(email));
tbir.appendChild(document.createTextNode(bir));
var aETxt = document.createElement("a");
aETxt.setAttribute("href","#");
aETxt.appendChild(document.createTextNode("编辑|"));
//---
var aDTxt = document.createElement("a");
aDTxt.setAttribute("href","#");
aDTxt.appendChild(document.createTextNode("删除"));
toper.appendChild(aETxt);
toper.appendChild(aDTxt);
aETxt.onclick = function(){
var tr1 = aETxt.parentNode.parentNode;
var arr = tr1.childNodes;
document.getElementById("name").value=arr[0].childNodes[0].nodeValue;
document.getElementById("sex").value=arr[1].childNodes[0].nodeValue;
document.getElementById("email").value=arr[2].childNodes[0].nodeValue;
document.getElementById("bir").value=arr[3].childNodes[0].nodeValue;
var updateUser = document.getElementById("updateUser");
updateUser.onclick = function (){
users.replaceChild(addUser(),tr1);
}
}
aDTxt.onclick = function(){
aDTxt.parentNode.parentNode.parentNode.removeChild(aDTxt.parentNode.parentNode);
}

//往行中添加
tr1.appendChild(tname);
tr1.appendChild(tsex);
tr1.appendChild(temail);
tr1.appendChild(tbir);
tr1.appendChild(toper);

return tr1;

}
function addUser1(){
var myForm = document.getElementById("addForm");
for(var i=0;i<myForm.length-3;i++){
if(i !=1){
var items = myForm.elements[i];
if(items.value.length==0){
alert("请正确输入!");
return false;
}else{
users.appendChild(addUser());
tusers.appendChild(users);
return true;
}
}
}
}
</script>
如有问题,欢迎留言!

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值