我有一个包含选择器HTML元素的数组。看起来像这样:
var selection = $('.features_items');
// selection = [div.features_item, div.features_item];
div看起来像这样:
<div class="features_item selected" value="2">
现在我想从数组中的div获取所有值:[2,3]
答案 0 :(得分:3)
您可以使用jQuery.map
在一个步骤中将jQuery对象映射到数组:
var values = $.map(selection, function() {
return $(this).val();
}); // values is now an array equal in length to 'selection'
答案 1 :(得分:1)
你想要这样的东西吗?
var values = [];
$('.features_items').each(function()
{
values.push( this.getAttribute('value') );
});
alert(values);