javascript - jquery var 中未传递数据属性值

标签 javascript jquery

我有以下问题。 我正在从数据库生成多个数据,每个项目都有一个“删除”btn,其中有一个带有条目 ID 号的“data-entryid”属性。

当我单击垃圾桶图标时,我想传递每个项目的数据属性,但使用当前的 js 代码,我只能获得第一个条目的 id。

这是我的 html btn 代码,其中“entry-num”是从数据库生成的条目 ID:

<a href="#" id="dlt-btn" data-entryid="{entry-num}" class="btn btn-danger delete">
   <i class="far fa-trash-alt"></i>
</a>

这是我的 js 代码,我(目前)正在尝试在控制台中传递值:

$("#dlt-btn").click(function(){
 var entryId = $(this).data("entryid");
 console.log(entryId);
});

最佳答案

The id global attribute defines a unique identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).

使用class而不是id:

$(".delete").click(function(){
  var entryId = $(this).data("entryid");
  console.log(entryId);
});

关于javascript - jquery var 中未传递数据属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52082058/

相关文章:

javascript - 在 Qunit 测试中触发 JavaScript 事件

javascript - 没有相同维度的 JQuery 可排序网格功能

javascript - 根据对象内的值获取对象键

javascript - 在javascript匹配中获取正则表达式的单独部分

javascript - 当文本变长时,我的 d3 y 轴溢出,如何在图表中打破单词?

javascript - Mongoose:查询年份列表

javascript - 防止动画进行时多次点击(stopPropagation & :animated)

php - Jquery 航路点水平滚动问题

javascript - 创建一个 cookie 来控制 Jquery 切换的内容

javascript - PHP 等待 Javascript 调用