我有以下问题。 我正在从数据库生成多个数据,每个项目都有一个“删除”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/