问题
ok so this one was tricky for me...
i have a dynamically created drop down and if the option "Other" is in that drop down i want to show div #optionalmess
<select class="VariationSelect" style="width: 95px;">
<option value="">Select Size</option>
<option value="1">Example</option
<option value="21">Other</option>
</select>
so if .variationselect contains the option "Other" (or value="21") show #optionalmess
if "other" (value="21") is not in the drop down, i want to hide #optionalmess
im on the right track but im a noob when it comes to writing it..
please help! =)
回答1:
You can use the contains selector, in conjunction with toggle() for this:
$('#optionalmess').toggle(
$(".VariationSelect option:contains('Other')").length > 0
);
回答2:
You can use .toggle(bool) for the hide/show with a condition, like this:
$("#optionalmess").toggle($(".VariationSelect option[value=21]").length>0);
This looks for any <option>
with a value of 21 under .VariationSelect
and checks the .length to see if any elements matched that selector.
来源:https://stackoverflow.com/questions/4160234/jquery-if-select-has-option-1-show-div