获取缓存的jquery对象的各个元素

时间:2011-05-03 16:28:26

标签: jquery select option selected

var obj = $("form select");
alert(obj.find(":selected:eq(0)").val()); // works
alert(obj.find(":selected:eq(1)").val()); // Does Not Work

如何使用我的缓存obj从其余选择元素中找到所选选项?

var n = 2;
var nthSelected = $(":selected",obj[n]); //Seems to work

访问缓存对象的各个元素也可以使用.filter()完成,如下所示:

alert(obj.filter(":eq(1)").val());

2 个答案:

答案 0 :(得分:0)

var selectedValues = [];

obj.each(function() {
  var selected = $(':selected', this).val();
  selectedValues.push(selected);
});

//now you have an array of all the selected values.  :)

答案 1 :(得分:0)

这样的事情应该有效:

var selects = $('form select');
var selected = $(':selected', selects);