【问题标题】:Jquery, search and show a hidden divJquery,搜索并显示隐藏的div
【发布时间】:2020-05-29 12:17:27
【问题描述】:

我正在制作一份杂货清单,您可以在其中搜索杂货并将其添加到您的清单中。我的杂货商品是从 MySQL 表中填充的,文本被添加到名为“foodcard”的 div 中。我的 Jquery 脚本几乎完全按照我的意愿工作,除了所有 150 多张卡片都是可见的,并且只有当您开始在输入框中输入时,卡片才会隐藏并在相关搜索词中淡出。我无法弄清楚如何在页面加载时隐藏卡片并仅显示搜索词。生病给你我所有的代码,但请告诉我是否需要包含更多

$(document).ready(function(){

 
  $('#grcsearch').on('keyup', function () {
    var grcsearch = $('#grcsearch').val().toLowerCase();    
   
   $('.foodcard').each(function(){
    var $this = $(this);
    
    if($this.text().toLowerCase().indexOf(grcsearch) === -1)
    $this.closest('.foodcard').fadeOut();
    else $this.closest('.foodcard').fadeIn();

   })     
  
   });         
  });
.wrapper {
    text-align: center;
  
    
}

.grocerylistcard {
    width: 85%;
    height: 90%;
    border-radius:15px;
    padding-bottom: 20px;   
    padding-top: 20px;
    padding-left: 30px;
    padding-right: 30px;
    border-width: 2px;
    border: solid 2px;
    border-color: rgb(199, 185, 201);
    background-color: #D5CAD6;
    margin: auto;
    display: inline-block;
    box-shadow: 5px 5px 3px grey;
}

.pccontainer1 {
   display: flex;
   flex-direction: row;
   }
   
   .foodcard {
    width: 15%;
    background-color: rgb(236, 231, 203);
    margin: 10px;
    margin-right: 10px !important;
    margin-left: 10px !important;
    
    border-radius: 10px;
    display: inline-table;
          
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta http-equiv="Expires" content="-1">


    <title>J & K Life Assist</title>
    <!-- MDB icon -->
    <link rel="icon" href="img/mdb-favicon.ico" type="image/x-icon">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.2/css/all.css">
    <!-- Google Fonts Roboto -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap">
    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Material Design Bootstrap -->
    <link rel="stylesheet" href="css/mdb.min.css">
    <!-- Your custom styles (optional) -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- custom font awesome kit-->
    
      <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>



<div class="wrapper">
        <div class="grocerylistcard">
            <h3 id="bptitle"> Grocery List </h3>
            <p>Search and add groceries we need to get on our next trip out!</p>
            <hr class="my-4">

            <div class="md-form form-lg" id="grocerysearchwrap">
                <input type="text" id="grcsearch" class="form-control form-control-lg">
                <label for="grocerysearch">Search for Groceries</label>
                
            </div>

     
            <!-- Central Modal Small -->
            <br />

<!-- this is where my PHP sript loads the SQL table items, but heres some hand typed cards for your reference -->
<div class="pccontainer1">

            <div class="foodcard">
            
              Apples
            </div>
            <div class="foodcard">
              
              Banana's
            </div>
            <div class="foodcard">
              
              Oranges
            </div>
            <div class="foodcard">
           
              Butter
            </div>
            <div class="foodcard">
              
              Milk
            </div>
                   
           </div> <!--pc container-->

           <div class="pccontainer1">

            <div class="foodcard">
              
              Bread
            </div>
            <div class="foodcard">
           
              Ramen
            </div>
            <div class="foodcard">
             
              Chicken
            </div>
            <div class="foodcard">
              
              Ground Beef
            </div>
            <div class="foodcard">
             
              Cheese
            </div>
                   
           </div> <!--pc container-->
<!-- grocery items above -->
        </div>
        <!--grocery card ends here-->
    </div> <!-- customcard wrapper-->
  • 我对这个社区很陌生。谢谢你的帮助!

【问题讨论】:

    标签: javascript jquery search


    【解决方案1】:

    我做了两处改动:

    在css中设置初始状态

    .foodcard{display: none}
    

    在 JS 中,为文本空添加条件

     grcsearch.trim().length === 0
    

    $(document).ready(function(){
    
     
      $('#grcsearch').on('keyup', function () {
        var grcsearch = $('#grcsearch').val().toLowerCase();    
       
       $('.foodcard').each(function(){
        var $this = $(this);
        
        if($this.text().toLowerCase().indexOf(grcsearch) === -1 || grcsearch.trim().length === 0)
        {
           $this.closest('.foodcard').fadeOut();}
        else $this.closest('.foodcard').fadeIn();
    
       })     
      
       });         
      });
    .wrapper {
        text-align: center;
      
        
    }
    
    .grocerylistcard {
        width: 85%;
        height: 90%;
        border-radius:15px;
        padding-bottom: 20px;   
        padding-top: 20px;
        padding-left: 30px;
        padding-right: 30px;
        border-width: 2px;
        border: solid 2px;
        border-color: rgb(199, 185, 201);
        background-color: #D5CAD6;
        margin: auto;
        display: inline-block;
        box-shadow: 5px 5px 3px grey;
    }
    
    .pccontainer1 {
       display: flex;
       flex-direction: row;
       }
       
       .foodcard {
        width: 15%;
        background-color: rgb(236, 231, 203);
        margin: 10px;
        margin-right: 10px !important;
        margin-left: 10px !important;
        
        border-radius: 10px;
        display: inline-table;
              
    }
    
    .foodcard{display: none}
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <meta http-equiv="Expires" content="-1">
    
    
        <title>J & K Life Assist</title>
        <!-- MDB icon -->
        <link rel="icon" href="img/mdb-favicon.ico" type="image/x-icon">
        <!-- Font Awesome -->
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.2/css/all.css">
        <!-- Google Fonts Roboto -->
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap">
        <!-- Bootstrap core CSS -->
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <!-- Material Design Bootstrap -->
        <link rel="stylesheet" href="css/mdb.min.css">
        <!-- Your custom styles (optional) -->
        <link rel="stylesheet" href="/css/style.css">
        <!-- custom font awesome kit-->
        
          <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    </head>
    
    
    
    <div class="wrapper">
            <div class="grocerylistcard">
                <h3 id="bptitle"> Grocery List </h3>
                <p>Search and add groceries we need to get on our next trip out!</p>
                <hr class="my-4">
    
                <div class="md-form form-lg" id="grocerysearchwrap">
                    <input type="text" id="grcsearch" class="form-control form-control-lg">
                    <label for="grocerysearch">Search for Groceries</label>
                    
                </div>
    
         
                <!-- Central Modal Small -->
                <br />
    
    <!-- this is where my PHP sript loads the SQL table items, but heres some hand typed cards for your reference -->
    <div class="pccontainer1">
    
                <div class="foodcard">
                
                  Apples
                </div>
                <div class="foodcard">
                  
                  Banana's
                </div>
                <div class="foodcard">
                  
                  Oranges
                </div>
                <div class="foodcard">
               
                  Butter
                </div>
                <div class="foodcard">
                  
                  Milk
                </div>
                       
               </div> <!--pc container-->
    
               <div class="pccontainer1">
    
                <div class="foodcard">
                  
                  Bread
                </div>
                <div class="foodcard">
               
                  Ramen
                </div>
                <div class="foodcard">
                 
                  Chicken
                </div>
                <div class="foodcard">
                  
                  Ground Beef
                </div>
                <div class="foodcard">
                 
                  Cheese
                </div>
                       
               </div> <!--pc container-->
    <!-- grocery items above -->
            </div>
            <!--grocery card ends here-->
        </div> <!-- customcard wrapper-->

    【讨论】:

      【解决方案2】:

      第一步是测试 grcsearch 值是否为空字符串。您可以通过以下方式实现:

      grcsearch.trim().length == 0
      

      此条件必须与 if 条件为 OR:

      if(grcsearch.trim().length == 0 || $this.text().toLowerCase().indexOf(grcsearch) === -1)
      

      keyup 事件结束时,您可以在 dom 就绪时触发该事件,只需触发它:

      .trigger('keyup');
      

      $('#grcsearch').on('keyup', function () {
          var grcsearch = $('#grcsearch').val().toLowerCase();
      
          $('.foodcard').each(function(){
              var $this = $(this);
      
              if(grcsearch.trim().length == 0 || $this.text().toLowerCase().indexOf(grcsearch) === -1)
                  $this.closest('.foodcard').fadeOut();
              else {
                  $this.closest('.foodcard').fadeIn();
              }
          })
      }).trigger('keyup');
      .wrapper {
          text-align: center;
      
      
      }
      
      .grocerylistcard {
          width: 85%;
          height: 90%;
          border-radius:15px;
          padding-bottom: 20px;
          padding-top: 20px;
          padding-left: 30px;
          padding-right: 30px;
          border-width: 2px;
          border: solid 2px;
          border-color: rgb(199, 185, 201);
          background-color: #D5CAD6;
          margin: auto;
          display: inline-block;
          box-shadow: 5px 5px 3px grey;
      }
      
      .pccontainer1 {
          display: flex;
          flex-direction: row;
      }
      
      .foodcard {
          width: 15%;
          background-color: rgb(236, 231, 203);
          margin: 10px;
          margin-right: 10px !important;
          margin-left: 10px !important;
      
          border-radius: 10px;
          display: inline-table;
      
      }
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      
      
      <div class="wrapper">
          <div class="grocerylistcard">
              <h3 id="bptitle"> Grocery List </h3>
              <p>Search and add groceries we need to get on our next trip out!</p>
              <hr class="my-4">
      
              <div class="md-form form-lg" id="grocerysearchwrap">
                  <input type="text" id="grcsearch" class="form-control form-control-lg">
                  <label for="grocerysearch">Search for Groceries</label>
      
              </div>
      
      
              <!-- Central Modal Small -->
              <br />
      
              <!-- this is where my PHP sript loads the SQL table items, but heres some hand typed cards for your reference -->
              <div class="pccontainer1">
      
                  <div class="foodcard">
      
                      Apples
                  </div>
                  <div class="foodcard">
      
                      Banana's
                  </div>
                  <div class="foodcard">
      
                      Oranges
                  </div>
                  <div class="foodcard">
      
                      Butter
                  </div>
                  <div class="foodcard">
      
                      Milk
                  </div>
      
              </div> <!--pc container-->
      
              <div class="pccontainer1">
      
                  <div class="foodcard">
      
                      Bread
                  </div>
                  <div class="foodcard">
      
                      Ramen
                  </div>
                  <div class="foodcard">
      
                      Chicken
                  </div>
                  <div class="foodcard">
      
                      Ground Beef
                  </div>
                  <div class="foodcard">
      
                      Cheese
                  </div>
      
              </div> <!--pc container-->
              <!-- grocery items above -->
          </div>
          <!--grocery card ends here-->
      </div> <!-- customcard wrapper-->

      【讨论】:

        【解决方案3】:

        您可以在 CSS 文件中的 .foodcard 上使用 display: none;,以便在页面加载之前隐藏元素

        然后你可以在你的JS文件中使用.show().hide() jquery函数来显示/隐藏元素

        【讨论】:

        • 谢谢,但是页面加载时卡片似乎会隐藏,但是一旦您输入搜索词然后清除搜索词,所有卡片都会重新填充,直到页面重新加载。
        • @TechEng 很奇怪,你会在 CSS 文件之前加载 JS 文件吗?
        【解决方案4】:

        您可以在文档就绪回调中隐藏所有卡片:

        $(document).ready(function(){
           $('.foodcard').hide()
        ...
        }
        
        // For handling clearing search text
        $('#grcsearch').change(function () {
            if ($(this).val().trim() == '') {
              $(".foodcard").hide()
            }
        })
        

        【讨论】:

        • 这样一旦页面加载就可以工作,但是一旦您清除搜索词并点击输入框,所有卡片都会重新填充,直到您重新加载页面。
        猜你喜欢
        • 2013-01-20
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-25
        • 2016-07-23
        相关资源
        最近更新 更多