获取表的ID

时间:2018-08-24 10:03:24

标签: jquery html

我有一个ID为'myrequest'的表,每行在const target = $('.WarningCount').parent(); target.css('cssText', target.attr('style')+'margin-left: -1.4% !important;'); 中都有一个ID。我想检查是否从myRequest表中单击了该值。

<td>
$(document).ready(function() {
  $(document).on('click', '.viewPopLink', function() {
    if ($(this).parent("table").has("[id^='myRequest']").length > 0) {
      // alert('request table');
      console.log('request table');
    } else {
      // alert('approval table');
      console.log('approval table');
    }
  });
});

3 个答案:

答案 0 :(得分:0)

在这里找到解决方案

使用closest代替parent,然后查找属性attr(id)

$(document).ready(function() {
  $(document).on('click', '.viewPopLink', function() {
    if ($(this).closest("table").attr("id") === "myRequest") {
      // alert('request table');
      console.log('request table');
    } else {
      // alert('approval table');
      console.log('approval table');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='myRequest'>
  <thead>
    <th>request ID</th>
    <th>name</th>
  </thead>
  <tbody>
    <tr>
      <td class="request-edit viewPopLink">1819AM0047</td>
      <td class="request-edit viewPopLink">aaa</td>
    </tr>
    <tr>
      <td class="request-edit viewPopLink">1819AM0048</td>
      <td class="request-edit viewPopLink">bbb</td>
    </tr>
  </tbody>
</table>

希望这对您有帮助

答案 1 :(得分:0)

您可以像这样将事件绑定在“ viewPopLink”上

$(document).ready(function(){
     $(".viewPopLink").on("click", function() {
         if ($(this).closest('table').attr('id') == "myRequest") {
            console.log('request table');
         } else {
            console.log('approval table');
         }
     });
 });

答案 2 :(得分:0)

内部文档准备功能写入此代码。 console.log(“ id”,$('#myRequest')。attr('id'));