点击表中的行删除按钮后,删除表行(Remove table row after clicking t

2019-06-17 11:51发布

解决方案可以使用jQuery或将普通的JavaScript。

我想删除一个表行用户点击包含在表行单元格因此,例如相应的按钮后:

<script>
function SomeDeleteRowFunction() {
 //no clue what to put here?
}
</script>

<table>
   <tr>
       <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
   </tr>
   <tr>
       <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
   </tr>
   <tr>
       <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
   </tr>
</table>

Answer 1:

您可以使用jQuery的click ,而不是使用onclick属性,尝试以下方法:

$('table').on('click', 'input[type="button"]', function(e){
   $(this).closest('tr').remove()
})

演示



Answer 2:

你可以做这样的:

<script>
    function SomeDeleteRowFunction(o) {
     //no clue what to put here?
     var p=o.parentNode.parentNode;
         p.parentNode.removeChild(p);
    }
    </script>

    <table>
       <tr>
           <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this)"></td>
       </tr>
       <tr>
           <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this)"></td>
       </tr>
       <tr>
           <td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this)"></td>
       </tr>
    </table>


Answer 3:

下面的解决方案是工作的罚款。

HTML:

<table>
  <tr>
    <td>
      <input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this);">
    </td>
  </tr>
  <tr>
    <td>
      <input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this);">
    </td>
  </tr>
  <tr>
    <td>
      <input type="button" value="Delete Row" onclick="SomeDeleteRowFunction(this);">
    </td>
  </tr>
</table>

JQuery的:

function SomeDeleteRowFunction(btndel) {
    if (typeof(btndel) == "object") {
        $(btndel).closest("tr").remove();
    } else {
        return false;
    }
}

我已经做了垃圾箱上http://codebins.com/bin/4ldqpa9



Answer 4:

使用纯JavaScript:

不需要通过this来的SomeDeleteRowFunction()

<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>

的onclick功能:

function SomeDeleteRowFunction() {
      // event.target will be the input element.
      var td = event.target.parentNode; 
      var tr = td.parentNode; // the row to be removed
      tr.parentNode.removeChild(tr);
}


文章来源: Remove table row after clicking table row delete button