我涨 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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?