javascript - 使用 javascript 和 php 的动态文本框

标签 javascript php jquery

你好,我是 javascript 和 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">

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

最佳答案

Use element.parentNode.parentNode.remove(); to remove element as you will have to find the tr element with respective clicked button

试试这个:

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">

关于javascript - 使用 javascript 和 php 的动态文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35511113/

相关文章:

javascript - 如何使用c# asp.net上传默认文件

javascript - 使用 HTML、CSS 和 JS 构建桌面应用程序的替代方案

php - Wordpress:在管理选项页面上传图片

php - 显示链接的绝对位置

javascript - 滚动时调整 Logo 大小

javascript - Canvas 错误 - 在 Chrome 中无法旋转 90 度

javascript - 如何对透明图像进行逐像素碰撞测试?

php - Codeigniter insert_batch 限制问题

javascript - 将垂直居中图像的标题左对齐

javascript - JQuery 增量/减量值取决于选定的复选框