- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > ASP >
- asp批量添加修改刪除操作示例代碼
核心代碼:
<title>asp批量添加修改刪除操作示例</title> <% if request.Form("op")="update" then'表單提交 ids=request.Form("ids") if ids<>"" then response.Write "要刪除的數據id集合:"&ids&"<br>" '=========數據庫刪除操作 conn.execute("delete from xxx where id in("&ids&")")'自己注意做安全驗證,假定id為數字集合,自己正則RegExp判斷有效性,pattern為^\d+(,\d+)*$ end if rows=request.Form("name").count'提交的數據行數據,包括添加/修改的 for i=1 to rows'遍歷每行數據 id=request.Form("id").item(i)&"" name=request.Form("name").item(i) sex=request.Form("sex").item(i) age=request.Form("age").item(i) addr=request.Form("addr").item(i) if id<>"" then'修改操作,如果id為數字加isnumeric判斷 response.Write "要修改數據行:"&id&"|"&name&"|"&sex&"|"&age&"|"&addr&"<br>" '修改操作 else'添加操作 response.Write "要添加數據行:"&name&"|"&sex&"|"&age&"|"&addr&"<br>" '添加操作 end if next end if %> <form method="post" onsubmit="return check(this)"> <input type="hidden" name="ids" /><!--用于存儲要刪除記錄的id集合--> <input type="hidden" name="op" value="update" /> <table border="1" id="tb" > <tr><th>姓名</th><th>性別</th><th>年齡</th><th>地址</th><th>刪除</th></tr> <!-------要修改的數據,自己讀取數據庫生成,<input type="hidden" name="id" value=""/>存儲id--------> <tr> <td><input type="text" value="姓名1" name="name" /></td> <td><input type="text" value="性別1" name="sex" /></td> <td><input type="text" value="年齡1" name="age" /></td> <td><input type="text" value="地址1" name="addr" /></td> <td><input type="button" value="刪除" onclick="removeRow(this)" /><input type="hidden" name="id" value="1"/></td> </tr> <tr> <td><input type="text" value="姓名2" name="name" /></td> <td><input type="text" value="性別2" name="sex" /></td> <td><input type="text" value="年齡2" name="age" /></td> <td><input type="text" value="地址2" name="addr" /></td> <td><input type="button" value="刪除" onclick="removeRow(this)" /><input type="hidden" name="id" value="2"/></td> </tr> <!-------要修改的數據示例結束--------> <tr><td colspan="5" align="center"><input type="submit" value="提交"/> <input type="button" value="添加新數據行" onclick="addRow()" /></td></tr> </table> </form> <script type="text/javascript"> function removeRow(btn){ if (confirm('確認刪除?!')) { var tr=btn.parentNode.parentNode; var id = btn.nextSibling;//注意刪除按鈕和id這個(gè)hidden控件之間不要有空格,要不nextSibling在標準瀏覽器下是空白節點(diǎn) if (id.value != '') {//刪除是存在的行而不是新增的,則id存儲到ids中 btn.form.ids.value += (btn.form.ids.value == '' ? '' : ',') + id.value; } tr.parentNode.removeChild(tr); } } function addRow() { var tb = document.getElementById('tb'), tr = tb.insertRow(tb.rows.length - 1),td=tr.insertCell(0); td.innerHTML = '<input type="text" name="name" />'; td = tr.insertCell(1); td.innerHTML = '<input type="text" name="sex" />'; td = tr.insertCell(2); td.innerHTML = '<input type="text" name="age" />'; td = tr.insertCell(3); td.innerHTML = '<input type="text" name="addr" />'; td = tr.insertCell(4); td.innerHTML = '<input type="button" value="刪除" onclick="removeRow(this)" /><input type="hidden" name="id" />';//新增數據行id為空 } function check(f) { var tb = document.getElementById('tb'), ipts; for (var i = 1, j = tb.rows.length - 1; i < j; i++) {//輸入驗證,去掉第一行表頭和最后一行操作 ipts = tb.rows[i].getElementsByTagName('input'); if (ipts[0].value == '') { alert('請輸入姓名!'); ipts[0].focus(); return false; } if (ipts[1].value == '') { alert('請輸入性別!'); ipts[1].focus(); return false; } if (ipts[2].value == '') { alert('請輸入年齡!'); ipts[2].focus(); return false; } if (ipts[3].value == '') { alert('請輸入地址!'); ipts[3].focus(); return false; } } } </script>
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站