简体   繁体   English

jQuery数据返回未定义

[英]Jquery data returning undefined

I am trying to retrieve the data using jquery, but I keep on getting undefined as the value. 我正在尝试使用jquery检索数据,但我一直在获取未定义的值。 The html code is being echoed to the document by a PHP function. html代码被PHP函数回显到文档中。 Thanks 谢谢

<td data-id='332'>
  <i class="fa fa-trash delSubCat"></i>
  <a href='#'>Validate</a>
</td>

$(document).on('ready',function(){
  $(this).on('click',function(event){
    event.preventDefault();
    console.log($(this).parent().data("id"));
  });

});

There are two problem 有两个问题

1) You didn't follow the hierarchy of table and tr above td and therefore it is not getting rendered properly. 1)您没有遵循td上方的table和tr层次结构,因此无法正确呈现它。

2) You added listener on this instead of 2)您在此添加了侦听器,而不是

 <table>
      <tr>
    <td data-id='332'>
      <i class="fa fa-trash delSubCat"></i>
      <a href='#'>Validate</a>
    </td>
        </tr>
    </table>

JS JS

  $('a').on('click',function(event){
    event.preventDefault();
    console.log($(this).parent().data("id"));
  });

Working jsfiddle: 工作jsfiddle:

https://jsfiddle.net/Lbagfn7o/ https://jsfiddle.net/Lbagfn7o/

You are missing <table> tag that's why the parent of a becomes body because of invalid structure 你缺少<table>标签,这就是为什么母公司a变,因为无效的结构体

 $(document).on('ready',function(){ $('a').on('click',function(event){ event.preventDefault(); console.log($(this).parent().data('id')); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <td data-id='332'> <i class="fa fa-trash delSubCat"></i> <a href='#'>Validate</a> </td> </table> 

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

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