使用 JavaScript 和 PHP 的动态文本框


dynamical textbox using javascript and php

hello im newbie at JavaScript and jquery.我想使用 JavaScript 制作一个动态文本框,它可以添加和删除一行。当我按下添加按钮时,它运行良好。但是当我按删除时,它删除了我所有的表。

这是我的JavaScript函数和我的PHP代码:

<script type="text/javascript">
    function addProg(){
            document.getElementById("add_prog").innerHTML += "<tr><td><input type='date' class='form-control' name='tanggal[]'></td><td><input type='number' class='form-control' name='kuota[]'></td><td><input type='time' class='form-control' name='jam_mulai[]'></td><td><input type='button' class='btn btn-danger' onclick='hapus()' value='Hapus'></tr>";
    }
    function hapus() 
    {
        var x = document.getElementById("add_prog");
        x.remove(x.tr);
    }
</script>
<div class="container">    
        <center><h3>Form Pendaftaran 
        </h3><center><br>
            <table class="table table-bordered">
            <thead><tr>
            <td> a </td>
            <td> b </td>
            <td> c </td>
            </tr></thead>
            <tbody id="add_prog">
            <tr id="1">
            <td><input type="date" class="form-control" name="tanggal[]"></td>
            <td><input type="number" class="form-control" name="kuota[]"></td>
            <td><input type="time" class="form-control" name="jam_mulai[]"></td>
            <td><input type="button" class="btn btn-danger" onclick="hapus()" value="Hapus"></td>
            </tr>
            </tbody>
            </table>
            <input type="button" class="btn btn-default" onclick="addProg()" value="Tambah">

我不知道如何删除要删除的特定索引并将其写入我的脚本中。 有人可以告诉我该怎么做吗?

使用element.parentNode.parentNode.remove();删除元素,因为您必须使用相应的单击按钮找到tr元素

试试这个:

function addProg() {
  document.getElementById("add_prog").innerHTML += "<tr><td><input type='date' class='form-control' name='tanggal[]'></td><td><input type='number' class='form-control' name='kuota[]'></td><td><input type='time' class='form-control' name='jam_mulai[]'></td><td><input type='button' class='btn btn-danger' onclick='hapus(this)' value='Hapus'></tr>";
}
function hapus(element) {
  element.parentNode.parentNode.remove(); //document.getElementById('add_prog').removeChild(element.parentNode.parentNode);
}
<div class="container">
  <center>
    <h3>Form Pendaftaran 
        </h3>
    <center>
      <br>
      <table class="table table-bordered">
        <thead>
          <tr>
            <td>a</td>
            <td>b</td>
            <td>c</td>
          </tr>
        </thead>
        <tbody id="add_prog">
          <tr id="1">
            <td>
              <input type="date" class="form-control" name="tanggal[]">
            </td>
            <td>
              <input type="number" class="form-control" name="kuota[]">
            </td>
            <td>
              <input type="time" class="form-control" name="jam_mulai[]">
            </td>
            <td>
              <input type="button" class="btn btn-danger" onclick="hapus(this)" value="Hapus">
            </td>
          </tr>
        </tbody>
      </table>
      <input type="button" class="btn btn-default" onclick="addProg()" value="Tambah">

我认为

您要删除的是tbody元素的lastChild

这里有一些关于最后一个孩子的信息。