简体   繁体   中英

jQuery Dropdown List Contains Filter

I have two dropdown lists that filter content. The first one is the locations and the second one is the jobs. The first list filters the second. I'm using a :contains to read the string values that allow my filter to work. I'm running into a problem when I want to use two contains at once as a filter. Here is the code:

HTML

<div class="holder">
<label for="volunteerLocation">Where do you want to volunteer?</label><br>
<select id="locations">
    <option value="0">--Select a Campus--</option>
    <option value="5">Location 1</option>
    <option value="6">Location 2</option>
    <option value="7">Location 3</option>
</select>
</div>

<br />

<div class="holder">
<label for="volunteerJobs">In which area would you like to serve?</label><br />
<select id="jobs">
    <option value="1">Job 1 (Location 1)</option>
    <option value="2">Job 2 (Location 2)</option>
    <option value="3">Job 3 (Location 3)</option>
    <option value="4">Job 4 (All locations)</option>
</select>
</div>

Javascript

var select = $('#jobs');
var options = [];
$(select).find('option').each(function () {
    options.push({ value: $(this).val(), text: $(this).text() });
});
$(select).data('options', options);

$('#locations').change(function () {
    filterText = $("#locations option:selected").text();
    var optionList = $(select).empty().data('options');
    var j = 0;

    $.each(optionList, function (i) {
        var option = options[i];
        if (option.text.indexOf(filterText) !== -1) {
            if (j == 0) {
                $('#jobs').prepend("<option value=''>--Select a Job--</option>").val('');
                j++;
            };
            $(select).append(
                $('<option>').text(option.text).val(option.value)
            );
        }

        if (filterText == "--Select a Campus--") {
            $(select).append(
                $('<option>').text(option.text).val(option.value)
            );
        }
    })
})

Here is a JSLint of this so you can see it in action Full Example

I'm trying to get "Job 4" to show up on everything but the "Select a Campus" option. How do I do that?

instead of looping with .each every time location change, and going through exceptions, me would create an index upon page load

var locJobs=new Array();

then you fill it with your data, for example

locJobs['5']=new Array();
locJobs['5'] = ['job 1','job 2']

then on change

$("#jobs").html('<option>'+locJobs[$(this).val()].join('</option><option>')+'</option>');

if you need to add the value on the options of #jobs you'll have to complicate that snippet a bit. It shall be more efficient & also make maintenance much easier (no exceptions to deal with just an array to populate from whatever data source you are using) as you'll end up with a very flexible solution

nb: you declare var select = $("#jobs") but then you use $(select); that is a useless overhead use select directly

a convention to keep code clear is to add $ to any variable that is caching a jquery object :

 var $select=$("#select")

then you use $select.whtever(//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM