急!请问如何在表格中动态的增加名称自定的行??? table.insertRow().insertCell().appendChild(newInput); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你问的错了,行tr是没有名称name属性的,只有ID属性,你根据id属性就能得到这行的对象在table里增加一行的方法<HEAD></HEAD><SCRIPT LANGUAGE="JavaScript"><!--global_Flag = 1;function save(){ var trd=document.all.tabA.insertRow(); trd.ID = "newtr_"+global_Flag; trd.insertCell().innerHTML="777"; trd.insertCell().innerHTML="888"; trd.insertCell().innerHTML="999"; global_Flag++; alert("你新增的行的ID:"+trd.ID); }//--></SCRIPT><BODY><table id=tabA border=1><tr ><td nowrap>111</td><td nowrap>222</td><td nowrap>333</td></tr><tr ><td nowrap>444</td><td nowrap>555</td><td nowrap>666</td></tr></table><INPUT TYPE="button" value="go" onclick="save();"></BODY> function addRow(){var tblObj = document.getElementById("table1");if(tblObj.rows){alert(tblObj.rows.length);}else{alert('aaa');}//追加行var newRow = tblObj.insertRow();newRow.style.display = "";var cellNum = tblObj.rows[0].cells.length;//追加列for (colIndex = 0; colIndex < cellNum; colIndex++) {var newCell = newRow.insertCell();initTblCell(newCell);}}function initTblCell(cell){var lastCell = document.getElementById("table1").rows[0].cells[cell.cellIndex];cell.innerHTML = lastCell.innerHTML;alert(cell.innerHTML);if (cell.children != null && cell.children.length > 0) {for(childIndex = 0; childIndex < cell.children.length; childIndex++) {var child = cell.children[childIndex];switch(child.type) {case "text":child.value = "";break;case "checkbox":child.value = "";child.checked = false;break;}}}cell.className = lastCell.className;cell.align = lastCell.align;cell.height = lastCell.height;}function buttonFun(){var obj= document.getElementsByName("text4");for(i=1;i<obj.length;i++){alert(obj[i].value);}}function dellRow(){var obj= document.getElementById("table1");var objRow=obj.rows.length-1;alert(objRow);if(objRow != 0){obj.deleteRow(objRow);}} <table id="table1" name="table1" width="300"> <tr style="display:none"> <td width="100"> <input type="button" name="testBtn4" value="button" onclick=""/> </td> <td width="100"> <input type="text" name="text4" id="text4"/> </td> <td width="100"> 123 </td> </tr> <tr> <td width="100"> <input type="button" name="testBtn4" value="button" onclick="toExcel_Page()"/> </td> <td width="100"> <input type="text" name="text4" id="text4"/> </td> <td width="100"> 123 </td> </tr> </table> <input type="button" name="testAdd" id="testAdd" value="AddRow" onclick="addRow()"/> <input type="button" name="testAdd" id="testAdd" value="DellRow" onclick="dellRow()"/> <input type="button" name="testAdd2" id="testAdd2" value="GetTextValue" onclick="buttonFun()"/> 动态添加行如果是 copy上一行的话,他的name和上一行一样,上面的例子就是这样,如果是.insertCell().innerHTML="<tr id="" name=""> ...<tr>",这样添加的话,id, name就可以直接加,不过name没什么用 谢谢 fengruzhuo 和 mingxuan3000 .问题已经解决. 又学到了一些新的用法.是以前没接触到的. 初来CSDN.不晓得这分是怎么给你们啊? 这样进 CSDN社区起始点-->我的帖子-->管理-->给分结贴记住解决问题了要给分结贴哦,否则人品会降,呵呵 TO :mingxuan3000(铭轩) .insertCell().innerHTML= " <tr id= " " name= " " > ... <tr > "用这种方法插入是能实现名称的自定,可是一个表格如果有三行.这种方法在第一格插入以后就会转到下行去在插. 不知道你是怎么实现的? 还是把我的贴上来,我是这样写的.插进去有问题,你看看<script language="javascript">var i = 0; function addTR() { i++; var iTab = document.getElementById("iTab"); var iTr = iTab.insertRow(); var cellNum = iTab.rows[0].cells.length; for (colIndex = 0; colIndex < cellNum; colIndex++) { var newCell = iTr.insertCell(); //问题就这里啦,控制不了. newCell.innerHTML="<tr id='tr'"+i+"><td><input name=chk_1 type=checkbox id=chk_1 value=\"1\" /></td><td>-->"+i+"<--</td></tr>"; //全插进一格里去啦.如果分开写,比如说在第一格插什么,在第二格在插什么,这个应该可以实现.但有没有简洁的. } }</script><table width="200" height="54" border="1" align="center" id="iTab"> <tr id="tr0"> <td> <input name=chk_1 type=checkbox id=chk_1 value="1" /> </td> <td>0</td> </tr></table><input type="button" name="Submit" value="增加" onclick="addTR()" /> 简洁的?copy上一行,上面3的例子就是这样, 大侠进,这两种写法效果是一样的吧? jquery如何遍历获取table中某个td的属性? 关于javascript操作checkbox的问题 如何在原有的页面上弹出登录页面? 高手进啊 一段奇怪的翻页代码 求一正则表达式 在检验表单输入时,为什么弹不出错误信息 什么不填写 提交了反而 提示注册成功!!! 我想得到当前焦点所在的控件,能够实现吗? js中2005-05-02怎么转换为2005/5/2? 请问哪里可以找到多一点关于JS的方法? new RegExp()和/***/有什么关系?
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--global_Flag = 1;
function save()
{
var trd=document.all.tabA.insertRow();
trd.ID = "newtr_"+global_Flag;
trd.insertCell().innerHTML="777";
trd.insertCell().innerHTML="888";
trd.insertCell().innerHTML="999"; global_Flag++;
alert("你新增的行的ID:"+trd.ID);
}
//-->
</SCRIPT><BODY><table id=tabA border=1>
<tr >
<td nowrap>111</td>
<td nowrap>222</td>
<td nowrap>333</td>
</tr>
<tr >
<td nowrap>444</td>
<td nowrap>555</td>
<td nowrap>666</td>
</tr>
</table>
<INPUT TYPE="button" value="go" onclick="save();">
</BODY>
var tblObj = document.getElementById("table1");
if(tblObj.rows){
alert(tblObj.rows.length);
}else{
alert('aaa');
}
//追加行
var newRow = tblObj.insertRow();
newRow.style.display = "";
var cellNum = tblObj.rows[0].cells.length;//追加列
for (colIndex = 0; colIndex < cellNum; colIndex++) {
var newCell = newRow.insertCell();
initTblCell(newCell);
}
}
function initTblCell(cell){
var lastCell = document.getElementById("table1").rows[0].cells[cell.cellIndex];
cell.innerHTML = lastCell.innerHTML;
alert(cell.innerHTML);
if (cell.children != null && cell.children.length > 0) {
for(childIndex = 0; childIndex < cell.children.length; childIndex++) {
var child = cell.children[childIndex];
switch(child.type) {
case "text":
child.value = "";
break;
case "checkbox":
child.value = "";
child.checked = false;
break;
}
}
}
cell.className = lastCell.className;
cell.align = lastCell.align;
cell.height = lastCell.height;
}
function buttonFun(){
var obj= document.getElementsByName("text4");
for(i=1;i<obj.length;i++){
alert(obj[i].value);
}}
function dellRow(){
var obj= document.getElementById("table1");
var objRow=obj.rows.length-1;
alert(objRow);
if(objRow != 0){
obj.deleteRow(objRow);
}} <table id="table1" name="table1" width="300">
<tr style="display:none">
<td width="100">
<input type="button" name="testBtn4" value="button" onclick=""/>
</td>
<td width="100">
<input type="text" name="text4" id="text4"/>
</td>
<td width="100">
123
</td>
</tr>
<tr>
<td width="100">
<input type="button" name="testBtn4" value="button" onclick="toExcel_Page()"/>
</td>
<td width="100">
<input type="text" name="text4" id="text4"/>
</td>
<td width="100">
123
</td>
</tr>
</table>
<input type="button" name="testAdd" id="testAdd" value="AddRow" onclick="addRow()"/>
<input type="button" name="testAdd" id="testAdd" value="DellRow" onclick="dellRow()"/>
<input type="button" name="testAdd2" id="testAdd2" value="GetTextValue" onclick="buttonFun()"/>
如果是.insertCell().innerHTML="<tr id="" name=""> ...<tr>",这样添加的话,id, name就可以直接加,不过name没什么用
CSDN社区起始点-->我的帖子-->管理-->给分结贴记住解决问题了要给分结贴哦,否则人品会降,呵呵
用这种方法插入是能实现名称的自定,可是一个表格如果有三行.这种方法在第一格插入以后就会转到下行去在插. 不知道你是怎么实现的?
var i = 0;
function addTR()
{
i++;
var iTab = document.getElementById("iTab");
var iTr = iTab.insertRow(); var cellNum = iTab.rows[0].cells.length; for (colIndex = 0; colIndex < cellNum; colIndex++)
{
var newCell = iTr.insertCell();
//问题就这里啦,控制不了.
newCell.innerHTML="<tr id='tr'"+i+"><td><input name=chk_1 type=checkbox id=chk_1 value=\"1\" /></td><td>-->"+i+"<--</td></tr>";
//全插进一格里去啦.如果分开写,比如说在第一格插什么,在第二格在插什么,这个应该可以实现.但有没有简洁的.
}
}
</script>
<table width="200" height="54" border="1" align="center" id="iTab">
<tr id="tr0">
<td>
<input name=chk_1 type=checkbox id=chk_1 value="1" />
</td>
<td>0</td>
</tr>
</table><input type="button" name="Submit" value="增加" onclick="addTR()" />
copy上一行,上面3的例子就是这样,