我有一个 html 表,旁边有一个按钮,当用户单击该按钮时,我尝试从表中获取 id。 这是 HTML
<table id="table_domExtra2_pro" class="col m12 bordered striped centered ">
<thead>
<tr>
<th>No.</th>
<th>PROPIETARIO DEL INMUEBLE</th>
<th>REGIMEN DE PROPIEDAD</th>
<th>COMO SE ACREDITA LA <br>POSESION DE LA PROPIEDAD</th>
<th>SUPERFICIE DE <br>LA UNIDAD</th>
</tr>
</thead>
<tbody>
<tr>
<td class="count">
</td>
<td>
<input name="propietario" id="propietario" type="text" class="validate" required>
</td>
<td>
<input name="reg_propiedad" id="reg_propiedad" type="text" class="validate" required>
</td>
<td>
<input name="pose_propiedad" id="pose_propiedad" type="text" class="validate" required>
</td>
<td>
<input name="sup_unidad" id="sup_unidad" type="text" class="validate" required>
</td>
</tr>
</tbody>
</table>
<div class="add_icon col s1 m6 ">
<a id="btn_agregar_pro" class="btn waves-effect red">Agregar</a>
</div>
<div class="add_icon col s1 m6 ">
<a id="btn_eliminar_pro" class="btn waves-effect red">Eliminar</a>
</div>
这是js
$('#btn_eliminar_pro').on("click", function(){
var idTable3 = $(this).parent().closest("table").attr("id");
var trs4 = $("#"+idTable3+" tr").length;
if(trs4 > 2){
$("#table_domData_pro tr:last").remove();
$("#table_domExtra_pro tr:last").remove();
$("#table_domExtra2_pro tr:last").remove();
}
});
我尝试使用 .prev() 来,但我真的迷失了,我文档中还有其他表格,js 只是选择其他表格,而不是按钮的上一个表格,非常感谢您的帮助:D
最佳答案
使用 div
的 siblings()
查找最接近的
表。下面获取表的id
。我刚刚将 siblings()
添加到您现有的代码中。
var idTable3 = $(this).parent().siblings().closest("table").attr("id");
关于javascript - 使用外部按钮从表中获取 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46740605/