基本代码已写出来了,直接可以用调试。求助。动态增加一行数据,如果点击“复制”按钮,就把该行数据内容填写到新增的那行数据里。如果点“增加一行”则新增一行空白数据。

gandalf11 2017-01-22 03:12:56
基本代码已经弄出来了,求调试

‘’求帮我看一下如下脚本,动态增加一行数据,如果点击“复制”按钮,就把该行数据内容填写到新增的那行数据里。如果点“增加一行”则新增一行空白数据。
现在可以新增一行空白数据,但不知道怎么在点选“复制”后,新增一行数据,并把点选行的数据赋值到新行里。
本人对JAVASCRIPT不是很懂,求助。

<html>
<head>
<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
//document.write(ntd[i].innerHTML);
//alert(ntd[i].innerHTML);
}

}
/**//* 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>
</head>
<body>
<form name=frmUserInfo1 action="user_list1.asp" method=post>
<br>
<table id=tabUserInfo1 border=1 width="720" cellpadding="5" cellspacing="0" bordercolor="#E8E09F">
<tr>
<td><div align="center">姓 名</div></td>
<td><div align="center">密 码</div></td>
<td><div align="center">职 位</div></td>
<td><div align="center">访问权限</div></td>
<td><div align="center">操 作</div></td>
</tr>
<tr id=trUserInfo1>
<td id=tdUserInfo1><input id=username1 name=username1 value="hxb" ></td>
<td id=tdUserInfo1><input id=usersex1 name=usersex1 value="1234"></td>
<td id=tdUserInfo1><input id=userage1 name=userage value="xs"></td>
<td id=tdUserInfo1><input id=userlove1 name=userlove value="bb"></td>
<!--<td id=tdUserInfo1><input type="submit" value="复制" /></td>-->
<td id=tdUserInfo1><input type=button value="复制" onClick="addRow(document.all.tabUserInfo,null,1,1)"></td>
</tr>

</table>
</form>

<form name=frmUserInfo action="zbList.asp" method=post>
<br>
<table id=tabUserInfo border=1 width="720" cellpadding="5" cellspacing="0" bordercolor="#E8E09F">
<tr>
<td><div align="center">姓 名</div></td>
<td><div align="center">密 码</div></td>
<td><div align="center">职 位</div></td>
<td><div align="center">访问权限</div></td>
<td><div align="center">操 作</div></td>
</tr>
<!--<tr style="display:none" id=trUserInfo>-->
<tr style="display:none" id=trUserInfo>
<td id=tdUserInfo><input id=username name=username></td>
<td id=tdUserInfo><input id=userpass name=userpass></td>
<td id=tdUserInfo><input id=userzw name=userzw></td>
<td id=tdUserInfo><input id=userrole name=userrole></td>
<td id=tdUserInfo>
<img alt="删 除" onClick="deleteRow(document.all.tabUserInfo,1,this)"></td>
</tr>
<tr>
<td colspan="5">
<div align="right">
<input type=button value="增加一行" onClick="addRow(document.all.tabUserInfo,null,1,1)">
</div></td></tr>
</table>
<table border=1 width="720" cellpadding="5" cellspacing="0" bordercolor="#E8E09F" >
<tr><td><div align="center">
<input type="submit" value="提 交" name="tj" />
</div></td></tr>
</table>
</form>
</body>
</html>
...全文
149 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
看红色标注的部分 <td id=tdUserInfo1><input type=button value="复制" onclick="addRow(document.all.tabUserInfo,null,1,1,this)"></td> function addRow(tabObj, colNum, sorPos, targPos, targetBtn) { 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 = targetBtn ? targetBtn .parentNode.parentNode: 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 var ipt; 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 if (targetBtn && (ipt = ntd[i].getElementsByTagName('input')[0])) {//注意这里如果复制的行数据修改过不用此代码更新将会是原来的值 ipt.value = TDs[i].getElementsByTagName('input')[0].value } //document.write(ntd[i].innerHTML); //alert(ntd[i].innerHTML); } }
gandalf11 2017-01-22
  • 打赏
  • 举报
回复
能否帮我就在这代码上改?多谢
当作看不见 2017-01-22
  • 打赏
  • 举报
回复
clone() jquery 的clone() ,方法 $("你要复制的行").clone().append("你要添加的位置");

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧