Loop through options in javascript object
I have an javascript object that contains a select list. I'd like to loop through all options within the select. I'm trying the following:
$(this+' .form-select option').each(function() {
console.log(this);
});
A console.log of this shows that it contains the options but for all the option elements on the page not the options I've passed to it.
I get the following error "Uncaught Syntax error, unrecognized expression: [object Object]"
Any help would be great.开发者_JS百科
Use .find()
, like this:
$(this).find('option').each(function() { console.log(this); });
this
is an object so you can't use it in a string, you can however use it to find other elements relatively. If this
is already a jQuery object, just do:
this.find('option').each(function() { console.log(this); });
I'm guessing that this
isn't a string with a selector in it, in which case your statement is invalid. My guess is your jQuery selector should be something like:
$('.form-select option', $(this)).each(function(){
console.log(this);
});
精彩评论