简体   繁体   English

JQuery表选择最接近的空

[英]JQuery table select closest empty

I am trying to select a class which is from the same table (and row). 我试图选择一个来自同一个表(和行)的类。
If the user clicks on "Remove", I need to grab the text from "form_delete_id". 如果用户点击“删除”,我需要从“form_delete_id”中获取文本。
Unfortunately I get an empty string back. 不幸的是我回来了一个空字符串。 I tried "closest()" and "parent()" without luck. 我没有运气就试过“最近()”和“父()”。
This is my code: 这是我的代码:

<table class="responsive-table bordered striped">
  <thead>
    <tr>
      <th>id</th>
      <th>page</th>
      <th>parameter</th>
      <th>method</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="form_delete_id">2</td>
      <td class="form_delete_page">dqfsq</td>
      <td class="form_delete_parameter">qsdfqs</td>
      <td class="form_delete_method">post</td>
      <td class="form_delete_trigger"><a class="waves-effect waves-light btn red"><i class="material-icons left">delete</i>Remove</a></td>
    </tr>
  </tbody>
</table>

And the Javascript below it: 和它下面的Javascript:

<script>
$(".form_delete_trigger").click(function() {
  alert($(this).closest(".form_delete_id").text());
});
</script>

My code can also be found on JSFIDDLE 我的代码也可以在JSFIDDLE找到

.form_delete_id is sibling element of clicked .form_delete_id .You need to use .siblings() instead of .closest() : .form_delete_id是单击.form_delete_id兄弟元素。您需要使用.siblings()而不是.closest()

 $(function(){ $(".form_delete_trigger").click(function() { alert($(this).siblings(".form_delete_id").text()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="responsive-table bordered striped"> <thead> <tr> <th>id</th> <th>page</th> <th>parameter</th> <th>method</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td class="form_delete_id">2</td> <td class="form_delete_page">dqfsq</td> <td class="form_delete_parameter">qsdfqs</td> <td class="form_delete_method">post</td> <td class="form_delete_trigger"><a class="waves-effect waves-light btn red"><i class="material-icons left">delete</i>Remove</a></td> </tr> </tbody> </table> 

Try getting the parent and searching it for the delete id: 尝试获取父级并在其中搜索删除ID:

<script>
$(".form_delete_trigger").click(function() {
  alert($(this).parent().find(".form_delete_id").text());
});
</script>

or using sibiling 或使用sibiling

<script>
$(".form_delete_trigger").click(function() {
  alert($(this).siblings(".form_delete_id").text());
});
</script>

https://jsfiddle.net/t8okf8kc/ https://jsfiddle.net/t8okf8kc/

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

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