开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜