我涨 2015-05-16 13:00 采纳率: 87.5%
浏览 2102
已采纳

JavaScript 动态增加表格

这样写为什么不能动态增加一行?我应该怎样修改

function insertRows(){ var tempRow=0; var tbl=document.getElementById("dictTbl"); tempRow=tbl.rows.length; //获取当前table的行数 var Rows=tbl.rows;//类似数组的Rows var newRow=tbl.insertRow(tbl.rows.length);//插入新的一行 var Cells=newRow.cells;//类似数组的Cells for (i=0;i<4;i++)//每行的3列数据 { alert("进入"); var newCell=Rows(newRow.rowIndex).insertCell(Cells.length); alert("过去"); newCell.align="center"; switch (i) { case 0 : newCell.innerHTML="<td align=\"center\"><input type=\"checkbox\"/></td>";break; case 1 : newCell.innerHTML="<td align=\"center\" width=\"120px\">" + "</td>";break; case 2 : newCell.innerHTML="<td align=\"center\"\><input id=\"classifyName\" name=\"classifyName\" type=\"text\" size=\"50\" maxlength=\"25\"/></td>";break; case 3 : newCell.innerHTML="<td width=\"400px\"></td>";break; } } }

...

删除   添加选项
        <s:form name="Form1" id="Form1" method="post">
                <table cellpadding="0" cellspacing="0" border="0" id="table1" class="stdtable stdtablecb">
                    <colgroup>
                        <col class="con0" style="width: 4%"/>
                        <col class="con1" />
                        <col class="con0" />
                        <col class="con1" />
                    </colgroup>
                    <thead>
                        <tr>
                            <th class="head0"><input type="checkbox" class="checkall"/></th>                           
                            <th class="head1">分类级别</th>
                            <th class="head0">分类名称</th>
                            <th class="head1">继承父分类</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <th class="head0"><input type="checkbox" class="checkall"/></th>                            
                            <th class="head1">分类级别</th>
                            <th class="head0">分类名称</th>
                            <th class="head1">继承父分类</th>
                        </tr>
                    </tfoot>
                    <tbody id="dictTbl">                     
                        <s:if test="#request.classifyList!=null && #request.classifyList.size()>0"> 
                            <s:iterator value="%{#request.classifyList}" var="class">               
                            <tr>
                                <td align="center"><input type="checkbox"/></td>
                                <td align="center" width="120px"><s:property value="%{#class.rank}"/></td>
                                <td align="center">
                                    <input id="classifyName" name="classifyName" type="text" value="<s:property value="%{#class.classifyName}"/>" size="50" maxlength="25"/>
                                </td>
                                <td width="400px"><s:property value="%{#class.inherit}"/></td>
                            </tr>
                            </s:iterator>
                        </s:if>
                    </tbody>                    
                </table>
                <table style="margin-top: 20px;float: right;">
                     <tr>
                        <td>
                            <button class="radius3" id="savebutton" style="width: 70px;" onclick="">保存</button>
                                                    </td>
                     </tr>
                </table>
  • 写回答

7条回答 默认 最新

  • danielinbiti 2015-05-16 14:16
    关注
    <scritp>
     function insertRows(){ 
           var tempRow=0; 
           var tbl=document.getElementById("dictTbl"); 
           tempRow=tbl.rows.length; //获取当前table的行数 
           var Rows=tbl.rows;//类似数组的Rows 
           var newRow=tbl.insertRow(tbl.rows.length);//插入新的一行 
           var Cells=newRow.cells;//类似数组的Cells 
           for (i=0;i<4;i++){//每行的3列数据 { alert("进入"); 
                   var newCell=Rows[newRow.rowIndex].insertCell(Cells.length); 
                   alert("过去"); newCell.align="center"; 
                   switch (i) { 
                           case 0 : 
                           newCell.innerHTML="<td align=\"center\"><input type=\"checkbox\"/></td>";break; 
                           case 1 : 
                           newCell.innerHTML="<td align=\"center\" width=\"120px\">" + "</td>";break; 
                           case 2 : newCell.innerHTML="<td align=\"center\"\><input id=\"classifyName\" name=\"classifyName\" type=\"text\" size=\"50\" maxlength=\"25\"/></td>";break; 
                           case 3 : newCell.innerHTML="<td width=\"400px\"></td>";break; 
                     } 
           } 
     }
     </script>
     <body>
     <table>
        <tbody id='dictTbl'>
      </tbody>
    </table>
     </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题