开发者社区> 问答> 正文

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>

展开
收起
小旋风柴进 2016-03-18 09:11:19 2311 0
1 条回答
写回答
取消 提交回答
  • <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>
    2019-07-17 19:05:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript函数 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载