Iterate through a group of select controls and choose only the ones that have a specific selected value
I need to iterate through a group of select lists on my page and choose only those开发者_如何转开发 selects that have the selected option of "Yes".
Something like (which doesnt work):
$(".option-select option[value='yes']:selected").each(function () {
alert($(this).attr("id"));
});
You need the .parent()
, or to be safer(<optgroup>
for example), .closest()
, like this:
$(".option-select option[value='yes']:selected").closest('select').each(function() {
alert(this.id);
});
Or .filter()
, like this:
$(".option-select").filter(function() { return $(this).val() == "yes"; }).each(function() {
alert(this.id);
});
精彩评论