Jquery : If select has option 1 show div

▼魔方 西西 提交于 2020-01-16 05:19:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!