Find all elements that have a certain data attribute (regardless of value)

拜拜、爱过 提交于 2019-12-11 03:37:19

问题


I have a form with a bunch of text elements, some of which have a data attribute set.

I want to loop through all the elements that have that attribute, extracting the attribute.

I've created a fiddle here.

var textInputs = $(':text');
alert('found ' + textInputs.length + ' textInputs');

var datas = textInputs.find('[data-foo]');
alert('found ' + datas.length + ' datas');

I'm finding the text elements, but my selector on the data attribute is returning no elements.

Ideas would be helpful...


回答1:


The [data-foo] selector is correct, but you should use it in a filter, instead of in a find:

var datas = textInputs.filter('[data-foo]');

See working fiddle here




回答2:


http://jsfiddle.net/qjb3V/

     var datas = $(':text[data-foo]');


来源:https://stackoverflow.com/questions/24441156/find-all-elements-that-have-a-certain-data-attribute-regardless-of-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!