javascript - 如何在表中设置为默认值?
问题描述
我有一个 Html 表,其中每一行都有一个按钮。当我单击一个按钮时,它会再显示 2 个按钮,然后就消失了。当我单击其他行的按钮时,我试图隐藏显示的按钮,但我不知道该怎么做。
这是我的功能代码
$("#myTable").on('click', 'tbody tr #editAction', function (e) {
$(this).hide().closest("tr").find("#deleteAction").show();
$(this).hide().closest("tr").find("#saveAction").show();
});
解决方案
您应该将 id 更改为 class,如果您使用 id,则只有第一行将应用事件。将您的代码更改为
$(".editAction").click(function (e) {
$("tr").find(".deleteAction").hide();
$("tr").find(".saveAction").hide();
$("tr").find(".editAction").show();
//show delete and save
$(this).closest("tr").find(".deleteAction").show();
$(this).closest("tr").find(".saveAction").show();
//hide edit button
$(this).hide();
});
$(".editAction").click(function (e) {
$("tr").find(".deleteAction").hide();
$("tr").find(".saveAction").hide();
$("tr").find(".editAction").show();
//show delete and save
$(this).closest("tr").find(".deleteAction").show();
$(this).closest("tr").find(".saveAction").show();
//hide edit button
$(this).hide();
});
.deleteAction, .saveAction{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Edit</th>
<th></th>
<th></th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>
<button class='editAction'>Edit</button>
</td>
<td>
<button class='deleteAction'>Delete</button>
</td>
<td>
<button class='saveAction'>Save</button>
</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>
<button class='editAction'>Edit</button>
</td>
<td>
<button class='deleteAction'>Delete</button>
</td>
<td>
<button class='saveAction'>Save</button>
</td>
</tr>
</table>
</body>
</html>
推荐阅读
- angular - 垫筹码滚动问题
- oracle - Expdp :ORA-39171: 作业正在经历可恢复的等待。?
- java - SpringBoot应用程序上的Maven构建失败
- java - JAI Tiff 到 JPEG 的转换问题
- javascript - 找到一个可以容纳新时间戳的时隙并计算频率
- unit-testing - Mockito Predicate JPA 运行不正确
- python - 根据条件选择熊猫上的特定行
- python - 搅拌机中的函数 bpy.ops.mesh.primitive_vert_add() 出错
- android - 某些手机的图像分辨率正在发生变化
- private-key - 在python中为curve25519生成33字节的公钥