如何在每行单击按钮时从数组中删除任何数组值
我想从数组中删除行并删除该
值?
var users = []
$(document).ready(loadTable);
function loadTable() {
for (var i = 0; i < users.length; i++) {
for (var j = 0; j < users[i].length; j++) {
//console.log(users[i][j])
} {
continue;
}
}
}
$("#submit").on('click', function() {
var temp = [document.getElementById("id").value, document.getElementById("name").value]
users.push(temp)
loadTable($("tbody").append("<tr>" +
"<td>" + $("#id").val() + "</td>" +
"<td>" + $("#name").val() + "</td>" +
//"<td>"+<button href="javascript:void(0);" class="remCF1 btn btn-small btn-danger">Remove</button>+"</td>"+
"</tr>"));
console.log(users)
});
table,
th,
td {
border: 1px solid black;
}
<html>
<head>
<title>Hello</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<input type="text" name="id" id="id">
<input type="text" name="name" id="name">
<button type="button" id="submit">Add</button>
<table id="demo">
<tbody>
</tbody>
</table>
</body>
</html>
I want to delete row as well as delete that value from array but I want delete button on each row and need to delete any index value on button click
How can I insert delete button on each row and delete row as well as value from same array ?
var users = []
$(document).ready(loadTable);
function loadTable() {
for (var i = 0; i < users.length; i++) {
for (var j = 0; j < users[i].length; j++) {
//console.log(users[i][j])
} {
continue;
}
}
}
$("#submit").on('click', function() {
var temp = [document.getElementById("id").value, document.getElementById("name").value]
users.push(temp)
loadTable($("tbody").append("<tr>" +
"<td>" + $("#id").val() + "</td>" +
"<td>" + $("#name").val() + "</td>" +
//"<td>"+<button href="javascript:void(0);" class="remCF1 btn btn-small btn-danger">Remove</button>+"</td>"+
"</tr>"));
console.log(users)
});
table,
th,
td {
border: 1px solid black;
}
<html>
<head>
<title>Hello</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<input type="text" name="id" id="id">
<input type="text" name="name" id="name">
<button type="button" id="submit">Add</button>
<table id="demo">
<tbody>
</tbody>
</table>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在表中添加一列来添加按钮,然后添加
onclick
事件来删除记录来实现它。演示:
You can achieve it by adding a column in a table to add button and then add
onclick
event to delete the record.Demo :
您需要在每条新行添加的每个新行上添加一个删除按钮。然后添加
addeventListener
“单击”单击按钮,并在其中添加name
。 然后将代码添加到加载表行中,然后将是这样,在JS文件中添加函数
delete
并创建功能
更新ID
,以更新<<<代码>&lt; tr&gt; 并将输入更新为delrow
函数&lt; button&gt;
整体代码将如下
我认为可能会有更好,更短的方式。但是我尚未找到它
如何使用javaScript在表中删除表行
You need to add a remove button on each new line addition. Then add
addEventListener
"click" on the click button and add aname
in it. The code added to the load table line will be like thisThen, add a function
delete
in your js fileAnd create a function
update id
, to update the id in the<tr>
and update the input to thedelRow
function in the<button>
The overall code will be like this
I think there might be a better and shorter way. But I haven't found it yet
How to remove the table row in a table using JavaScript