国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 石泉县| 政和县| 阿拉善右旗| 马关县| 儋州市| 越西县| 贞丰县| 新民市| 资源县| 兴化市| 洪洞县| 庄浪县| 扶绥县| 甘孜县| 磐安县| 贡觉县| 黄浦区| 于都县| 潜江市| 府谷县| 葫芦岛市| 牙克石市| 清原| 昆明市| 漠河县| 佛坪县| 图们市| 长寿区| 辽阳县| 铜梁县| 信宜市| 突泉县| 赣榆县| 萍乡市| 南雄市| 宜丰县| 铁岭县| 兖州市| 夏河县| 邹平县| 水城县|