Javascript - 添加动态表行

时间:2018-03-11 07:59:33

标签: javascript html html-table

我尝试通过单击按钮创建动态表格行。问题是第一行被完美地添加,但之后开始嵌套在添加的原始行内。

HTML代码

<tr class="txtMult" id="addresses"></tr>

<a href="#" id="add" class="btn btn-success">+</a>

JS

<script>
  var count=2;
  $("#add").click(function(){
    var html="<td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][type]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][description]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][qty]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][unit_price]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][sub_total]' required readonly></div></td>";

    $("#addresses").append(html);
    count++;
  });
</script>

我做错了吗?还是我的逻辑坏了?

供参考 - 最终输出 enter image description here

3 个答案:

答案 0 :(得分:1)

您将所有内容附加到单个tr,因此所有内容都在一行中。你需要用tr包装插入的td并附加到tbody。

<table>
  <thead>
    etc...
  </thead>
  <tbody id="addresses">
  </tbody>
</table>


<a href="#" id="add" class="btn btn-success">+</a>

<script>
  var count=2;
  $("#add").click(function(){
    var html="<tr><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][type]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][description]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][qty]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][unit_price]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][sub_total]' required readonly></div></td></tr>";
    $("#addresses").append(html);
    count++;
  });
</script>

答案 1 :(得分:1)

首先,您添加的元素应为tr, 其次,您应该将其添加到tbody,因此我们选择tr的父级:

<script>
  var count=2;
  $("#add").click(function(){
    var html="<tr>... your old tds here ...</tr>";

    $("#addresses").parents("tbody").append(html);
    count++;
  });
</script>

答案 2 :(得分:1)

没有得到你的问题,但我认为这可能是你的答案

function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = "NEW CELL1";
    cell2.innerHTML = "NEW CELL2";
}
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
  <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
  </tr>
  <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
  </tr>
  <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
  </tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>



</body>
</html>