How to use jQuery selectors on jQuery wrapped arrays
I have an array filled with artificiality created option elements, the way I create them is the following:
var daysArr = new Array();
for(i=1; i<=31; i++){
daysArr.push('<option value="'+ i +'">'+ i +'</option>');
}
$(daysArr.join(''));
What I'm trying to do is to use a selector on this array, like that:
$(daysArr.join('')).find('option:lt(5)');
The only thing I got is an empty array, even for .find('option'); There is the following info in jQ documentation for lt() selector:
Select all elements at an index less than index within开发者_开发知识库 the matched set.
Mine array is an index type array. I'll be glad if someone can tell me from where comes the problem.
daysArr
is your array of options. Trying to find()
something in them will go down one level too deep.
It would work if you were to do something like this:
$('<select>' + daysArr.join('') + '</select>').find('option:lt(5)');
But of course a simpler way of achieving the same would be
daysArr.slice(0,5);
var daysArr = $('<select>');
for(i=1; i<=31; i++){
daysArr.append('<option value="'+ i +'">'+ i +'</option>');
}
daysArr.children('option:gt(5)')
精彩评论