国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > ASP > 正文

asp+jsp+JavaScript動態(tài)實現添加數據行

2024-05-04 10:59:26
字體:
來源:轉載
供稿:網友

在應用程序的開發(fā)中,有些輸入信息是動態(tài)的,比如我們要注冊一個員工的工作經歷,比如下圖

如果做成死的,只能填寫三個,如果是四個呢?或者更多呢,那不是添加不上去了嗎,所以這樣固然不好,我們可以用動態(tài)添加表格行實現,如下圖,添加一行,輸入一行信息,這樣比較靈活

下面我們就來看看如何在asp和asp.net中結合javascript來實現這種效果:
首先,動態(tài)添加表格是要在前臺實現的,當然后臺也可以,不過可能要用到ajax,很麻煩,所以最好采用javascript來實現,下面來介紹動態(tài)添加表格行的兩種方式:
第一種:源碼
Javascript:

 <script type="text/javascript"> /**//*This function is use to add one row dynamicly * tabObj : Target table * colNum: The number of columns that of a row in table * sorPos: The source of the new row. * targPos: The position where the new row will be added. * */ function addRow(tabObj,colNum,sorPos,targPos){ var nTR = tabObj.insertRow(tabObj.rows.length-targPos); // Insert a new row into appointed table on the //appointed position. var TRs = tabObj.getElementsByTagName('TR'); // Get TRs collection from the appointed table var sorTR = TRs[sorPos]; // Positioned the sorTR var TDs = sorTR.getElementsByTagName('TD'); // Get TDs collection from the appointed row if(colNum==0 || colNum==undefined || colNum==isNaN){ colNum=tabObj.rows[0].cells.length; } var ntd = new Array(); // Create a new TDs array for(var i=0; i< colNum; i++){ // Traverl the TDs in row ntd[i] = nTR.insertCell(); // Create new cell ntd[i].id = TDs[0].id; // copy the TD's id to new cell. | Attention! The TDs's //suffix must be appointed ntd[i].innerHTML = TDs[i].innerHTML; // copy the value in ntd[i]'s innerHTML from corresponding TDs } } /**//* This function is use to remove appointed row in appointed table * tabObj: the appointed table * targPos: target row position * btnObj: currently clicked delete image button * */ function deleteRow(tabObj,targPos,btnObj){ //Remove table row for(var i =0; i<tabObj.rows.length;i++){ if(tabObj.getElementsByTagName('img')[i]==btnObj){ tabObj.deleteRow(i+targPos); } } } </script>

Html

<table id=tabUserInfo border=1 width="720"> <tr>  <td>姓名</td> <td>性別</td> <td>年齡</td> <td>愛好</td><td>Delete</td> </tr> <tr style="display:none" id=trUserInfo> <td id=tdUserInfo><input id=username name=username ></td> <td id=tdUserInfo><input id=usersex name=usersex></td> <td id=tdUserInfo><input id=userage name=userage></td> <td id=tdUserInfo><input id=userlove name=userlove></td> <td id=tdUserInfo><img alt="Delete" onClick="deleteRow(document.all.tabUserInfo,1,this)"></td> </tr> <tr> <td><input type=button value="Add" onClick="addRow(document.all.tabUserInfo,null,1,1)"></td> </tr> </table>             
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌图县| 信丰县| 鱼台县| 卓尼县| 邳州市| 济宁市| 宁城县| 鄢陵县| 永康市| 泰和县| 通道| 璧山县| 宁武县| 合肥市| 新乡县| 青海省| 洱源县| 吉安市| 临汾市| 革吉县| 阿城市| 小金县| 丰顺县| 湖口县| 科技| 福清市| 蒲城县| 麦盖提县| 荔浦县| 保康县| 南涧| 昆山市| 井陉县| 山阴县| 浦城县| 九江市| 嘉黎县| 蓝山县| 碌曲县| 东城区| 东阳市|