使用jQuery在Bootstrap 4中过滤卡 [英] Filter cards in Bootstrap 4 with jQuery

查看:72
本文介绍了使用jQuery在Bootstrap 4中过滤卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Bootstrap 4的新卡组件创建一个由许多卡填充的页面.

I am attempting to create a page that is populated by many cards, using bootstrap 4's new card component.

我想创建一个搜索栏,当搜索时,它会过滤出标题与搜索查询不匹配的卡片.

I want to create a search bar, that when searched, filters out cards whose titles don't match the search query.

这里是我所想的大手笔. 柱塞

Here is a plunker of what I have in mind. Plunker

我希望卡片得到类似display: noneopacity:0的东西,如果它们不匹配.

I would like the cards to get something like a display: none, or opacity:0 if they don't match.

我当前正在尝试编写一个功能,搜索栏的onChange会执行此操作.如果可以解决,我会发布.

I currently am attempting to write a function that onChange of the search bar does this. I'll post if I can get it figured out.

我也尝试使用内置的代码段功能.

I've tried to use the built in snippet feature as well.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/js/bootstrap.js"></script>
<link href="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap.css" rel="stylesheet" />

<div class="container">
  <div class="row">
    <div class="col-sm-4">
      <input type="search" placeholder="Search......" name="search" class="searchbox-input" onkeyup="buttonUp();" required>
    </div>
    <div class="col-sm-4">
    </div>
    <div class="col-sm-4">
    </div>
  </div>
  <div class="card-columns">
    <div class="card">
      <div class="card-block">
        <h4 class="card-title">Card title that wraps to a new line</h4>
        <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
      </div>
    </div>
    <div class="card card-block">
      <blockquote class="card-blockquote">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
        <footer>
          <small class="text-muted">
          Someone famous in <cite title="Source Title">Source Title</cite>
        </small>
        </footer>
      </blockquote>
    </div>
    <div class="card">
      <div class="card-block">
        <h4 class="card-title">Card title</h4>
        <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
        <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
        </p>
      </div>
    </div>
    <div class="card card-block card-inverse card-primary text-xs-center">
      <blockquote class="card-blockquote">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat.</p>
        <footer>
          <small>
          Someone famous in <cite title="Source Title">Source Title</cite>
        </small>
        </footer>
      </blockquote>
    </div>
    <div class="card card-block text-xs-center">
      <h4 class="card-title">Card title</h4>
      <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
      <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
      </p>
    </div>
    <div class="card">
    </div>
    <div class="card card-block text-xs-right">
      <blockquote class="card-blockquote">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
        <footer>
          <small class="text-muted">
          Someone famous in <cite title="Source Title">Source Title</cite>
        </small>
        </footer>
      </blockquote>
    </div>
    <div class="card card-block">
      <h4 class="card-title">Card title</h4>
      <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p>
      <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small>
      </p>
    </div>
  </div>
</div>

推荐答案

下面是一个简单的示例,说明如何使用jQuery的包含选择器:

Here's a quick example of how you could do it using jQuery's contains selector:

$('.searchbox-input').change( function () {
    $('.card').show();
    var filter = $(this).val(); // get the value of the input, which we filter on
    $('.container').find(".card-title:not(:contains(" + filter + "))").parent().css('display','none');
});

当前,此操作是在更改搜索输入时发生的,您可能需要设置一个提交"按钮,并使其在提交"时触发.

Currently this is set up to happen on change of the search input, you would probably want set up a submit button and have it fire on submit instead.

这篇关于使用jQuery在Bootstrap 4中过滤卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆