繁体   English   中英

同时使用文本框和复选框和jQuery过滤数据?

[英]Filtering data using textbox and checkboxes simultaneously with jQuery?

我想通过使用jQuery与以下内容来过滤存储在段落中的景点:

  • 文本框,我在其中放置substring的吸引力始于
  • 复选框,其中应仅显示具有某些类别的景点。 当您勾选多个框时,它应显示具有任何列出类别的项目。

我已经做到了,但是这些过滤器不能同时工作。 一个过滤器会覆盖另一个过滤器的结果,因为它们在整个列表上起作用并分别在整个列表上调用show()hide()

HTML:

<h3>Search:</h3>
<div>
  <input type="text" id="search-box" class="form-control" placeholder="Search">
</div>
<h3>Categories:</h3>
<div id="options" class="checkbox">
  <label>
    <input type="checkbox" rel="club">Club</label>
  <label>
    <input type="checkbox" rel="other">Other</label>
</div>
<div id="attractions" style="font-size: x-large">
  <p class="club" style="display: block;"><a href="/TouristAttractions/1">Cocomo</a>
  </p>
  <p class="club" style="display: block;"><a href="/TouristAttractions/2">Princess</a>
  </p>
  <p class="club" style="display: block;"><a href="/TouristAttractions/3">Le Secret</a>
  </p>
  <p class="other" style="display: block;"><a href="/TouristAttractions/4">Wyspa piasek</a>
  </p>
  <p class="other" style="display: block;"><a href="/TouristAttractions/5">C# Operational Base</a>
  </p>
</div>

使用Javascript:

$('div.checkbox').delegate('input[type=checkbox]', 'change', function() {

  var $lis = $('#attractions > p').hide();
  var $checked = $('input:checked');

  if ($checked.length) {
    ($checked).each(function() {
      $lis.filter('.' + $(this).attr('rel')).show();
    }).find('input:checkbox').change();
  } else {
    $lis.show();
  }
});

$('#search-box').keyup(function() {
  var valThis = this.value.toLowerCase();

  $('div#attractions > p').each(function() {
    var text = $(this).text(),
      textL = text.toLowerCase();
    (textL.indexOf(valThis) === 0) ? $(this).show(): $(this).hide();
  });

});

我想必须有某种方法可以同时取得成果。 我会为我显示正确的方向而感激,甚至可能建议删除此代码并使用一些过滤器插件?

我认为这可以彻底解决您的问题。 注释中有解释。

//Triger filterAttractions when something changes.
$("#search-box, #options input").change(filterAttractions);
$("#search-box").keyup(filterAttractions);

function filterAttractions() {
    //Get the text of the textbox.
    var searchtext = $("#search-box").val();
    //Get an array with the rel attributes of the checked checkboxes.
    var categories = $("#options input:checked").map(function() {
        return $(this).attr("rel");
    }).get();
    //Perform this once for every attraction.
    $("#attractions p").each(function() {
        var attraction = $(this);
        //See if it has any of the checked categories.
        var category = false;
        for(i=0; i<categories.length; i++)
            if(attraction.hasClass(categories[i]))
                category = true;
        //See if it starts with the right text.
        var text = attraction.text().indexOf(searchtext) === 0
        //Show or hide the attraction depending on the result.
        attraction.toggle(category && text);
    });
}

小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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