javascript - 如何从元素类中获取数据值?

标签 javascript jquery html

HTML 代码:

<input type="file" name="efile" id="efile" data-cat="electronics" style="width:100%" class="alertme">
<input type="file" name="dfile" id="dfile" data-cat="Decoratives" style="width:100%" class="alertme">
<input type="file" name="kfile" id="kfile" data-cat="kitchen" style="width:100%" class="">
<input type="file" name="hdfile" id="hdfile" data-cat="Home" style="width:100%" class="">
<input type="file" name="ffile" id="ffile" style="width:100%" data-cat="Furnitures" class="alertme">

如何获取存在类alertme的data-cat属性值?感谢您阅读我的问题。我的英语很弱,所以如果我犯了任何语法或拼写错误,请向我道歉。

最佳答案

这是实现此目的的代码:

$(".alertme").each(function(){
    // Here you can fill your array or whatever you want..
    $(this).attr("data-cat"); 
});

关于javascript - 如何从元素类中获取数据值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40486067/

相关文章:

javascript - 在 Node.js 中调整图像大小的简单方法?

jquery - 是否有相当于 .closest() 的方法,可以向下而不是向上搜索 DOM 树?

javascript - 动态添加表单元素后应用javascript

javascript - 当您在已加载脚本标签或 jquery getScript 时调用它时,它是否会双重加载?

javascript - http-equiv ="content-language"不起作用(element.lang = "")

javascript - 如何在 Android 的 React Native 中以编程方式打开 Tez 应用程序(印度的 Google Pay 应用程序)?

javascript - bootbox 确认 - 选择“确定”时包括 href

jquery - 当我使用 jQuery sortable 并开始拖动时,如何防止我的 li 高度在 IE8 中发生变化

javascript - CSS3 和 JavaScript 打印区域

javascript - 为什么添加一个矩形然后设置其左侧和顶部会导致可选的 'ghost' 矩形保留在旧位置?