开发者

Jquery : If select has option 1 show div

开发者 https://www.devze.com 2023-01-24 11:18 出处:网络
I have a dynamically created drop down and if the option "Other" is in that drop down i want to show div #optionalmess

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

精彩评论

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