javascript - 使用外部按钮从表中获取 id

标签 javascript jquery html

我有一个 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

最佳答案

使用 divsiblings() 查找最接近的 表。下面获取表的id。我刚刚将 siblings() 添加到您现有的代码中。

var idTable3 = $(this).parent().siblings().closest("table").attr("id");

关于javascript - 使用外部按钮从表中获取 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46740605/

相关文章:

javascript - 调整 Base64 图像大小在 Firefox 中不起作用

javascript - jquery:下拉菜单不显示固定位置 div 顶部的边框样式

javascript - WinJS 停止内存中的 javascript

jquery - 实现交叉淡入淡出的图像效果

html - CSS:在 Chrome 上转换 1 个像素偏移

javascript - 如何从 setTimeout 做出 promise

javascript - 如何使用 Javascript 计算一般树中子级的总和

c# - 关注 TextBox 正在使用 C# .net 触发 PageLoad

javascript - 外部链接打不开

jquery - tinyscrollbar.js 的键盘控制