单击表行删除按钮后删除表行

lik*_*nee 21 javascript jquery row html-table

解决方案可以使用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>
Run Code Online (Sandbox Code Playgroud)

und*_*ned 37

您可以使用jQuery click而不是使用onclick属性,请尝试以下操作:

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

演示


小智 27

你可以这样做:

<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>
Run Code Online (Sandbox Code Playgroud)


A-S*_*ani 8

使用纯 Javascript:

不需要传递thisSomeDeleteRowFunction()

<td><input type="button" value="Delete Row" onclick="SomeDeleteRowFunction()"></td>
Run Code Online (Sandbox Code Playgroud)

点击功能:

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);
}
Run Code Online (Sandbox Code Playgroud)


gau*_*171 5

以下解决方案工作正常.

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>
Run Code Online (Sandbox Code Playgroud)

JQuery的:

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

我在http://codebins.com/bin/4ldqpa9上做过垃圾箱