Find all elements that have a certain data attribu

2019-07-21 02:47发布

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...

2条回答
可以哭但决不认输i
2楼-- · 2019-07-21 03:15

http://jsfiddle.net/qjb3V/

     var datas = $(':text[data-foo]');
查看更多
我想做一个坏孩纸
3楼-- · 2019-07-21 03:32

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

查看更多
登录 后发表回答