问题
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