javascript - jQuery 通过两个选择输入按数据属性进行过滤

标签 javascript jquery html css

我在通过两个不同的输入使用 jQuery 过滤结果 div 时遇到问题。用户可以决定按办公室专业办公室和专业进行过滤。过滤是根据 div 上与选择输入值相对应的数据属性设置的。

<div>
  <label for="officeSearch">Search by office:</label>
  <select name="Office Search" id="officeSearch">
    <option value="all"></option>
    <option value="communication">Communication</option>
    <option value="internal medicine">Internal Medicine</option>
  </select>
</div>
<div>
  <label for="specialtySearch">Search by specialty:</label>
  <select name="Specialty Search" id="specialtySearch">
    <option value="all"></option>
    <option value="Bone Cancer">Bone Cancer</option>
    <option value="Breast Cancer">Breast Cancer</option>
    <option value="Oral Cancer">Oral Cancer</option>
  </select>
</div>

<div class="module-sm profile" data-office="communication" data-specialty="Oral Cancer">
  <p>Person A</p>
</div>
<div class="module-sm profile" data-office="communication" data-specialty="Breast Cancer">
  <p>Person B</p>
</div>
<div class="module-sm profile" data-office="internal medicine" data-specialty="Bone Cancer">
  <p>Person C</p>
</div>

这是我正在使用的 jQuery,它在选择更改时触发:

$(document).ready(function() {
  $("#officeSearch").on('change', function(){
    var selectedOffice = $('#officeSearch').val();
    var selectedSpecialty = $('#specialtySearch').val();
    var person = $('#filterList .profile').not('.out');
    var allPersons = $('#filterList .profile');
    var allPersonsOffice = $('#filterList .profile').data('office');
    var allPersonsOut = $('#filterList .profile.out');

    var office = $('.profile[data-office="' + selectedOffice +'"]');

    alert(''+ selectedOffice + ' ' + selectedSpecialty +'');

    if (selectedOffice == 'all' && selectedSpecialty == 'all'){
        $(allPersons).removeClass('out').fadeIn(500);
    }
    else {
        $(person).not(office).addClass('out').fadeOut(500);
        office.removeClass('out').fadeIn(500);
    }
  });
  $("#specialtySearch").on('change', function(){
    var selectedOffice = $('#officeSearch').val();
    var selectedSpecialty = $('#specialtySearch').val();
    var person = $('#filterList .profile').not('.out');
    var allPersons = $('#filterList .profile');
    var allPersonsOffice = $('#filterList .profile').data('office');
    var allPersonsOut = $('#filterList .profile.out');

    var specialty = $('.profile[data-specialty="' + selectedSpecialty +'"]');

    alert(''+ selectedOffice + ' ' + selectedSpecialty +'');

    if (selectedOffice == 'all' && selectedSpecialty == 'all'){
        $(allPersons).removeClass('out').fadeIn(500);
    }
    else {
        $(person).not(specialty).addClass('out').fadeOut(500);
        specialty.removeClass('out').fadeIn(500);
    }
  });
});

如果有帮助,我做了一个 codepen展示我正在尝试做的事情以及我目前所处的位置。

我已经做了一些搜索,并且几周来一直在绞尽脑汁地思考如何让它发挥作用。非常感谢任何使此代码更加简洁的帮助或其他人如何解决此问题的示例!

最佳答案

  1. 从任一选择更改中调用单个更新。
  2. 根据选择创建过滤器(已附加)。
  3. 隐藏不在匹配项中的内容
  4. 显示匹配项。

JSFiddle:http://jsfiddle.net/TrueBlueAussie/2u7NY/

$(document).ready(function () {
    var onChange = function () {

        var selectedOffice = $('#officeSearch').val();
        var selectedSpecialty = $('#specialtySearch').val();
        var filter = "#filterList .profile";
        var allPersons = $(filter);
        if (selectedOffice != "all")
        {
            filter += '[data-office="' + selectedOffice + '"]'
        }
        if (selectedSpecialty != "all")
        {
            filter += '[data-specialty="' + selectedSpecialty + '"]'
        }
        var $matching = allPersons.filter(filter);
        $(allPersons).not($matching).removeClass('out').fadeOut(500);
        $matching.removeClass('out').fadeIn(500);        
    }

    $("#officeSearch, #specialtySearch").on('change', onChange );
});

更新:http://jsfiddle.net/TrueBlueAussie/2u7NY/2/

过滤器可以稍微提高效率,因为不需要“#filterList .profile”来根据属性过滤allPersons

我还删除了函数变量并将函数内联放置在更改事件上。

$(document).ready(function () {
    $("#officeSearch, #specialtySearch").on('change',
        function () {
            var selectedOffice = $('#officeSearch').val();
            var selectedSpecialty = $('#specialtySearch').val();
            var allPersons = $("#filterList .profile");
            var filter = "";
            if (selectedOffice != "all") {
                filter = '[data-office="' + selectedOffice + '"]'
            }
            if (selectedSpecialty != "all") {
                filter += '[data-specialty="' + selectedSpecialty + '"]'
            }
            var $matching = allPersons.filter(filter);
            $(allPersons).not($matching).removeClass('out').fadeOut(500);
            $matching.removeClass('out').fadeIn(500);
        });
});

关于javascript - jQuery 通过两个选择输入按数据属性进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23832258/

相关文章:

html - 如何将内容放入框中并使其响应?

javascript - 使用 css 和 js 动态创建六边形设计

javascript - 安装 npm 包 weexpack 时校验和验证失败

php - 使用 AJAX/PHP 将其他网站表格中的内容插入到我的表单中?

jquery - .prop() 返回对象而不是值

javascript - 添加图像链接会破坏 jquery slider ,请帮忙!

javascript - Jquery 使用单独的闪烁文本计数器

javascript - 在顶部添加元素时保持滚动位置在 Firefox 中有效,但在 Chrome 中无效

javascript - 解析 Google Stock XML

javascript - 是否有类似 `.map` 的对象函数?使用相同的键创建新对象