javascript實(shí)現(xiàn)信息增刪改查的方法
本文實(shí)例講述了javascript實(shí)現(xiàn)信息增刪改查的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<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><input type="checkbox" name="chbk" id="chbk1" onclick="selectAll()"/></th>
<th>名稱</th>
<th>性別</th>
<th>郵箱</th>
<th>出生日期</th>
<th>操作</th>
</tr>
</thead>
<tbody id="users">
</tbody>
</table>
<div id="pages"></div>
</div>
<div style="border: 1px blue solid;">
<form action="">
<table id="divs">
<tbody id="addUsers">
<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="添加日期" onclick="showCalender(this,document.all.bir)"/>
</td>
</tr>
<tr id="addu">
<td colspan="2"><input type="button" value="添加" onclick="addUser()" id="add"/></td>
</tr>
<tr id="addu1">
<td colspan="2"><input type="button" value="修改" id="upduser" /></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</body>
<script>
window.onload = function(){
alert("onload");
document.getElementById("addu1").style.display = "none";
}
function selectAll(){
var users = document.getElementById("users");
var ips = users.getElementsByTagName("input");
var chbk = document.getElementById("chbk1");
for(var i=0;i<ips.length;i++){
ips[i].setAttribute("checked",chbk.getAttribute("checked"));
}
}
function addUser(){
alert("add");
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").Value;
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");
var cbk1 = document.createElement("input");
cbk1.setAttribute("type","checkbox");
cbk1.setAttribute("name","chbk");
cbk.appendChild(cbk1);
tname.appendChild(document.createTextNode(name));
tsex.appendChild(document.createTextNode(sex));
temail.appendChild(document.createTextNode(email));
tbir.appendChild(document.createTextNode(bir));
var adelete = document.createElement("a");
var aupdate = document.createElement("a");
adelete.setAttribute("href","#");
aupdate.setAttribute("href","#");
adelete.appendChild(document.createTextNode("刪除|"));
aupdate.appendChild(document.createTextNode("修改"));
toper.appendChild(adelete);
toper.appendChild(aupdate);
tr1.appendChild(cbk);
tr1.appendChild(tname);
tr1.appendChild(tsex);
tr1.appendChild(temail);
tr1.appendChild(tbir);
tr1.appendChild(toper);
var users = document.getElementById("users");
users.appendChild(tr1);
tusers.appendChild(users);
adelete.onclick = function(){
users.removeChild(adelete.parentNode.parentNode);
}
aupdate.onclick function(){
document.getElementById(addu).style.display = "none";
document.getElementById(addu1).style.display = "block";
var utr = aupdate.parentNode.parentNode;
var utrs = utr.childNodes;
document.getElementById("name").value = utrs[1].innerHTML;
document.getElementById("sex").value = utrs[2].innerHTML;
document.getElementById("email").value = utrs[3].innerHTML;
document.getElementById("bir").value = utrs[4].innerHTML;
var upUser = document.getElementById("upduser");
upUser.onclick = function(){
utr.childNodes[1].innerHTML = document.getElementById("name").value;
utr.childNodes[2].innerHTML = document.getElementById("sex").value;
utr.childNodes[3].innerHTML = document.getElementById("email").value;
utr.childNodes[4].innerHTML = document.getElementById("bir").value;
document.getElementById("addu1").style.display = "none";
document.getElementById("addu").style.display = "block";
}
}
testPage()
}
var indexPage = document.createElement("a");
var upPage = document.createElement("a");
var downPage = document.createElement("a");
var endPage = document.createElement("a");
var nowpage = 1;
function testPage(){
var tbodyUsers = document.getElementById("users");
var trUsers = document.getElementById("tr");
var countRecord = trUsers.length;
var PAGESIZE = 2;
var countPage = (countRecord%PAGESIZE ==0?countRecord/PAGESIZE:Math.ceil(countRecord/PAGESIZE));
var pages=document.getElementById("pages");
if(!pages.hasChildNodes()){
getPages(nowpage);
}
index.onclik=function(){
noepage=1;
indexPageInfo(countRecord,trUsers);
}
upPage.onclick=function(){
if(nowpage-1>1){
nowpage-=1;
}else{
nowpage=1;
indexPageInfo(countRecord,trUsers);
}
var startindex =(nowpage-1)*PAGESIZE;
var endindex=startindex+PAGESIZE;
PageInfo(startindex,endindex,countRecord,trUsers);
}
downPage.onclick=function(){
if(nowpage+1>=countPage){
nowpage=countPage;
}else{
nowpage=+1;
}
var startindex =(nowpage-1)*PAGESIZE;
var endindex=startindex+PAGESIZE;
PageInfo(startindex,endindex,countRecord,trUsers);
}
endPage.onclick=function(){
if(nowpage>1){
var startindex =(nowpage-1)*PAGESIZE;
for(var i=0;i<countRecord;i++){
if(i<startindex){
trUsers[i].style.display="none";
}else{
trUsers[i].style.display="block";
}
}
}else{
indexPageInfo(countRecord,trUsers);
}
}
}
function indexPageInfo(countRecord, trUsers) {
if (countRecord <= 2) {
for ( var i = 0; i < PAGESIZE; i++) {
trUsers[i].style.display = "block";
}
} else {
for ( var i = 2; i < countRecord; i++) {
trUsers[i].style.display = "none";
}
}
}
function PageInfo(startindex, endindex, countRecord, trUsers) {
for ( var i = 0; i < countRecord; i++) {
if (i >= startindex && i < endindex) {
trUsers[i].style.display = "block";
} else {
trUsers[i].style.display = "none";
}
}
}
function getPages(numpage) {
indexPage.appendChild(document.createTextNode("首頁"));
indexPage.setAttribute("href", "#");
upPage.appendChild(document.createTextNode("上一頁"));
upPage.setAttribute("href", "#");
downPage.appendChild(document.createTextNode("下一頁"));
downPage.setAttribute("href", "#");
endPage.appendChild(document.createTextNode("末頁"));
endPage.setAttribute("href", "#");
var pages = document.getElementById("pages");
pages.appendChild(indexPage);
pages.appendChild(upPage);
pages.appendChild(downPage);
pages.appendChild(endPage);
}
</script>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- js 如何實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的增刪改查
- javascript操作xml(增刪改查)例子代碼hta版
- javascript Xml增刪改查(IE下)操作實(shí)現(xiàn)代碼
- JS對(duì)select控件option選項(xiàng)的增刪改查示例代碼
- JavaScript針對(duì)網(wǎng)頁節(jié)點(diǎn)的增刪改查用法實(shí)例
- node.js操作mysql(增刪改查)
- AngularJS中如何使用$http對(duì)MongoLab數(shù)據(jù)表進(jìn)行增刪改查
- Node.js操作mysql數(shù)據(jù)庫增刪改查
- javascript js 操作數(shù)組 增刪改查的簡(jiǎn)單實(shí)現(xiàn)
- JS結(jié)合bootstrap實(shí)現(xiàn)基本的增刪改查功能
相關(guān)文章
基于微信小程序?qū)崿F(xiàn)人臉數(shù)量檢測(cè)的開發(fā)步驟
最近項(xiàng)目需求是統(tǒng)計(jì)當(dāng)前攝像頭中的人臉個(gè)數(shù),所以下面這篇文章主要給大家介紹了關(guān)于基于微信小程序?qū)崿F(xiàn)人臉數(shù)量檢測(cè)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
layer.confirm點(diǎn)擊第一個(gè)按鈕關(guān)閉彈出框的方法
今天小編就為大家分享一篇layer.confirm點(diǎn)擊第一個(gè)按鈕關(guān)閉彈出框的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
javascript設(shè)計(jì)模式之中介者模式Mediator
這篇文章主要介紹了javascript設(shè)計(jì)模式之中介者模式Mediator,需要的朋友可以參考下2014-12-12
JavaScript高級(jí)程序設(shè)計(jì) 閱讀筆記(二十一) JavaScript中的XML
雖然XML和DOM已經(jīng)變成Web開發(fā)的重要組成部分,但目前僅IE跟Mozilla支持客戶端的XML處理2012-09-09
JavaScript使用IEEE 標(biāo)準(zhǔn)進(jìn)行二進(jìn)制浮點(diǎn)運(yùn)算產(chǎn)生莫名錯(cuò)誤的解決方法
javascript做帶小數(shù)的計(jì)算時(shí),會(huì)出現(xiàn)9的循環(huán),以下方法幫助解決。2011-05-05

