Jquery : If select has option 1 show div
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>
If .variationsele开发者_如何学编程ct
contains the option "Other" (or value="21") show #optionalmess
If "other" (value="21") is not in the drop down, I want to hide #optionalmess
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.
You can use the contains selector, in conjunction with toggle() for this:
$('#optionalmess').toggle(
$(".VariationSelect option:contains('Other')").length > 0
);
精彩评论