开发者

jQuery filter select options

开发者 https://www.devze.com 2023-03-28 01:03 出处:网络
Is it possible to filter specific select options? I have a code : <select id=\"adcategory\" name=\"adcategory\">

Is it possible to filter specific select options?

I have a code :

<select id="adcategory" name="adcategory">
    <option value="">Select</option>
    <option value="25" class="dropdownparentcategory">Florida Atlantic University</option>

    <option value="26">- Books </option>
    <option value="27">- Electronics</option>
    <option value="28">- For Rent</option>

    <option value="17" class="dropdownparentcategory">Florida International University</option>

    <option value="18">- Books</option>
    <option value="19">- Electronics</option>
    <option value="20">- For Rent</option>

    <option value="1" class="dropdownparentcategory">Florida Tech</option>
    <option value="2">- Books</option>
    <option value="3">- Electronics</option>
    <option value="7">- For Rent</option>


</select>

So if the variable for example $school = Florida Atlantic University than show only thoes options witch is till next school category ( 开发者_开发问答.dropdownparentcategory ), so in this case they would be only :

<select id="adcategory" name="adcategory">
    <option value="">Select</option>

    <option value="26">- Books </option>
    <option value="27">- Electronics</option>
    <option value="28">- For Rent</option>

</select>

As you can see in this image :

jQuery filter select options

Is it possible to create with jQuery?


This looks like potentially bad design. I would suggest moving to <optgroup> [MDN] tags to group your drop down items:

<select>
    <optgroup label="Florida Atlantic University">
        <option value="1">Text</option>
        <option value="2">Text</option>
        <option value="3">Text</option>
    </optgroup>
    <optgroup label="Florida Tech">
        <option value="4">Text</option>
        <option value="5">Text</option>
        <option value="6">Text</option>
    </optgroup>
</select>

Then it's trivially easy to select child elements;

$('optgroup[label*="' + yourSchoolName + '"]').find('option')


Sure is.

$("#adcategory option:contains('Florida International University')").nextUntil(".dropdownparentcategory")

http://jsfiddle.net/Xeon06/YEeMH/


i would say, add classes for all your element by categorizing

<option class="js_atl" value="25" />
<option class="js_atl" value="26" />
<option class="js_atl" value="27" />
<option class="js_int" value="28" />
<option class="js_int" value="29" />

so like that you can use ur $school = "Florida Atlantic University" show or append or what ever with $(".js_atl") for the other category $(".js_int") which returns array of selected elements in that class category.

hope this helps.


You should be able to apply a style of "display: none;" to the tags you don't want visible.

0

精彩评论

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

关注公众号