简体   繁体   English

如何在 JavaScript 中单击按钮时从 DOM 中删除一行?

[英]How to delete a row from DOM on button click in JavaScript?

I have an HTML code as shown below.我有一个HTML 代码,如下所示。 I have copied this from DOM and it is generated through php.我已经从 DOM 复制了它,它是通过 php 生成的。

<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
    <button type="button" onclick="removeRow()" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AEFA">
    <input type="text" name="en_desc[]" value="Foreign Affairs and International Trade">
</div>

<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
    <button type="button" onclick="removeRow()" name="delete_row[]" style="margin-right:10px;" value="">Delete</button>
    <input type="text" name="code[]" style="margin-right:10px;" value="AGFO">
    <input type="text" name="en_desc[]" value="Agriculture and Forestry">
</div>

The above HTML code belongs to the following screenshot:以上HTML 代码属于以下截图:

在此处输入图片说明

Problem Statement:问题陈述:

I am wondering what JS code I need to add so that on clicking Delete button it deletes a row containing that button.我想知道我需要添加什么 JS 代码,以便在单击“删除”按钮时删除包含该按钮的行。

For example: If I click the 2nd row Delete button, it should delete the complete 2nd row and if I click the 1st row Delete button, it should delete the complete 1st row .例如:如果我点击第二行删除按钮,它应该删除完整的第二行,如果我点击第一行删除按钮,它应该删除完整的第一行

This is what I have tried:这是我尝试过的:

function removeRow() {
 document.queryselector(".house-senate-committee").remove();
}

You can use closest to achieve it like below您可以使用最接近来实现它,如下所示

 function removeRow(element) { $(element).closest('.house-senate-committee').remove(); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="house-senate-committee" style="text-align:center; margin-top:15px;"> <button type="button" onclick="removeRow(this)" name="delete_row[]" style="margin-right:10px;" value="">Delete</button> <input type="text" name="code[]" style="margin-right:10px;" value="AEFA"> <input type="text" name="en_desc[]" value="Foreign Affairs and International Trade"> </div> <div class="house-senate-committee" style="text-align:center; margin-top:15px;"> <button type="button" onclick="removeRow(this)" name="delete_row[]" style="margin-right:10px;" value="">Delete</button> <input type="text" name="code[]" style="margin-right:10px;" value="AGFO"> <input type="text" name="en_desc[]" value="Agriculture and Forestry"> </div>

If you change your buttons to have onclick="removeRow(this)" , you can then rewrite removeRow as below to remove that button's div when it is clicked.如果您将按钮更改为onclick="removeRow(this)" ,则可以按如下方式重写removeRow以在单击该按钮时删除该按钮的 div。

 function removeRow(el) { el.parentNode.remove(); }
 <div class="house-senate-committee" style="text-align:center; margin-top:15px;"> <button type="button" onclick="removeRow(this)" name="delete_row[]" style="margin-right:10px;" value="">Delete</button> <input type="text" name="code[]" style="margin-right:10px;" value="AEFA"> <input type="text" name="en_desc[]" value="Foreign Affairs and International Trade"> </div> <div class="house-senate-committee" style="text-align:center; margin-top:15px;"> <button type="button" onclick="removeRow(this)" name="delete_row[]" style="margin-right:10px;" value="">Delete</button> <input type="text" name="code[]" style="margin-right:10px;" value="AGFO"> <input type="text" name="en_desc[]" value="Agriculture and Forestry"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM