开发者

Iterate through a group of select controls and choose only the ones that have a specific selected value

开发者 https://www.devze.com 2023-01-20 05:14 出处:网络
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\".

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);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消