首页 > 解决方案 > 试图从数据属性中找到 id 并设置它

问题描述

我有数据表,其中没有我从表中获取的 ID

按照此:

https://jsfiddle.net/44f0pcx2/3/

我试图从中找到 id 这是我的结构

rowId: function(a) {
                console.log(a);
                console.log($(this).find('a.add').data('id'));
                return 'id_' + $(this).find('.add').data('id');
            }

我为每个 rw 得到的结果是这样的:

{phonenumber: "45454545", name: "bill", action: "<a href="javascript:;" class="add" data-id="13">Add</a>", …}

标签: javascriptjquerydatatables

解决方案


我找到了一种方法来做到这一点。它适用于片段..但我不知道它是否会大规模使用..你可以试试

  • 要获取 html 字符串,您可以使用a.action

var a = {phonenumber: "45454545", name: "bill", action: "<a href='javascript:;' class='add' data-id='13'>Add</a>"};

var GetId = $('<div/>').append(a.action).find('.add').data('id'); // create a div and append the `<a>` to it and get the attribute
console.log(GetId);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


推荐阅读