javascript - 试图从数据属性中找到 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>", …}
解决方案
我找到了一种方法来做到这一点。它适用于片段..但我不知道它是否会大规模使用..你可以试试
- 要获取 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>
推荐阅读
- excel - Excel for macOS - 从 VBA 访问 ComboBox
- tailwind-css - 有没有办法让链接在默认情况下带有下划线?
- wordpress - 如何为 WordPress 中的所有 cookie 添加 sameSite=None 属性标头?
- javascript - 如何将多个值添加到本地存储中的键
- javascript - 格式化日期时不考虑时间的时刻本地化?
- php - 当正则表达式测试似乎工作时,预匹配为空
- reactjs - 如何在 ReactJS 中第二次单击时撤消 OnClick 功能
- javascript - 使用脚本拆分 PDF,但不能使用横向 PDF
- swift - 在swift5中将对象列表中的值收集到字典中
- php - 无法使用 iPhone 上的媒体库通过表单上传文件