使用jQuery从元素选择中获取属性值作为数组

问题描述:

我使用jQuery有以下内容:

I have the following using jQuery:

var x = $('.boxes > input:checked');

我试图从x中检索一个id值数组,但无法弄清楚该怎么做.

From x I am trying to retrieve an array of id values and have been unable to work out how to do this.

类似的东西:

var y = x[id];
// y becomes an array like ['1', '2', '3'] assuming
// that x had 3 checkboxes with id's of 1, 2, 3 etc.

您可以使用 jQuery.map :

var x = $('.boxes > input:checked');

var y = $.map(x, function (element){
  return element.id;
});

y变量将是一个包含元素ID的数组.

The y variable, will be an array containing the element ids.