动态生成表格

来源:互联网 发布:js函数返回true false 编辑:程序博客网 时间:2024/06/05 00:23
<table id="optionContainer">
                     <tr id="option1">
                         <td><input type="text" class="form-control" placeholder="商品编号" style="margin-right:30px"></td>
                         <td><input type="text" class="form-control" placeholder="商品名称" style="margin-right:30px"></td>
                         <td><input type="text" class="form-control" placeholder="商品数量" style="margin-right:30px"></td>
                         <td><input type="text" class="form-control" placeholder="商品单价" style="margin-right:30px"></td>
                         <td><input type="button" value="添加" onclick="add()"></td>
                         <!-- <td><input type="button" value="删除" onclick="dele()"></td> -->
                     </tr>                   

    </table>


<script type="text/javascript">
    var rowCount=1;
    
    function add(){
      rowCount++;  
      var newRow='<tr id="option'+rowCount+'">'+
                   '<td><input type="text" class="form-control" placeholder="商品编号" style="margin-right:30px"></td>'+
                  '<td><input type="text" class="form-control" placeholder="商品名称" style="margin-right:30px"></td>'+
                  '<td><input type="text" class="form-control" placeholder="商品数量" style="margin-right:30px"></td>'+
                  '<td><input type="text" class="form-control" placeholder="商品单价" style="margin-right:30px"></td>'+
                  '<td><input type="button" value="添加" onclick="add()"></td>'+
                  '<td><input type="button" value="删除" onclick="dele(rowCount)"></td>';
     $('#optionContainer').append(newRow);  
    }
    
    function dele(rowIndex){
          $("#option"+rowIndex).remove();  
               rowCount--;    
    }
</script>

0 0