2017-07-21 205 views
-2

我不知道如何進行搜索時自動刷新結果,當鍵入時,如何在JQuery中實現搜索功能?

我也需要使它不區分大小寫,並且如果沒有結果來顯示段落class =「not-found」。 我不知道如何進行搜索時自動刷新結果,當鍵入時,

我也需要使它不區分大小寫,如果沒有結果顯示段落class =「not-found」。

var usersArray = [ 
 
    { 
 
     name: "Jhon Doe", 
 
     age: 21, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Jane Doe", 
 
     age: 20, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Milo Westfall", 
 
     age: 31, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Dayna Bennefield", 
 
     age: 27, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Danny Eckhoff", 
 
     age: 18, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Estella Fosdick", 
 
     age: 51, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Farah Benson", 
 
     age: 77, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Keith Gross", 
 
     age: 21, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Malcolm X", 
 
     age: 20, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Jhon Cena", 
 
     age: 31, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Rich Ross", 
 
     age: 27, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Charlie Sheen", 
 
     age: 44, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Lena Donovan", 
 
     age: 51, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Jay Kos", 
 
     age: 77, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Vincent Robert", 
 
     age: 21, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Adam Rodriguez", 
 
     age: 20, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Jhon Travolta", 
 
     age: 31, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Ben Mackferson", 
 
     age: 27, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Stella Cox", 
 
     age: 18, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Jenna Johnson", 
 
     age: 51, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
     name: "Bill Tim", 
 
     age: 77, 
 
     image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    } 
 
    ]
body { 
 
    margin: 0; 
 
    padding: 0; 
 
    font-family: 'Zilla Slab', serif; 
 
} 
 

 
.container { 
 
    width: 810px; 
 
    margin: 0 auto; 
 
} 
 

 
h2 { 
 
    float: left; 
 
} 
 

 
#search { 
 
    float: right; 
 
    margin-top: 30px; 
 
} 
 

 
.clear { 
 
    clear: both; 
 
} 
 

 
.not-found { 
 
    text-align: center; 
 
    display: none; 
 
} 
 

 
.users { 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
} 
 

 
.user-card { 
 
    flex: 0 0 auto; 
 
    flex-basis: 140px; 
 
    margin: 5px; 
 
    padding: 5px; 
 
    text-align: center; 
 
    border: 1px solid #eee; 
 
    border-radius: 5px; 
 
} 
 

 
.user-image img { 
 
    width: 140px; 
 
    height: 140px; 
 
    border-radius: 5px; 
 
} 
 

 
.user-info { 
 
    margin-top: 10px; 
 
} 
 

 
.user-info .name, .user-info .age { 
 
    margin: 0; 
 
} 
 

 
#template { 
 
    display: none; 
 
}
<!DOCTYPE html> 
 

 
    <html> 
 
    <head> 
 
    <meta charset="UTF-8"> 
 
    <title>Users loaded from JSON</title> 
 
    <link href="https://fonts.googleapis.com/css?family=Zilla+Slab" rel="stylesheet"> 
 
    <link rel="stylesheet" href="style.css"> 
 
    </head> 
 
    <body> 
 

 
    <div class="container"> 
 
     <h2>Users:</h2> 
 
     <input type="text" id="search" placeholder="Search by name..."> 
 

 
     <div class="clear"></div> 
 

 
     <div id="template" class="user-card"> 
 
      <div class="user-image"> 
 
       <img src=""> 
 
      </div> 
 
      <div class="user-info"> 
 
       <h4 class="name"></h4> 
 
       <h5 class="age"></h5> 
 
      </div> 
 
     </div> 
 

 
     <p class="not-found"> 
 
      No users found, please change search criteria... 
 
     </p> 
 

 
     <section class="users"> 
 
      <!-- Users loaded here --> 
 
     </section> 
 
    </div> 
 

 
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
 
    <script src="users.js"></script> 
 
    <script src="script.js"></script> 
 
    </body> 
 
    </html>

+0

這就是第三個環節 - nbNZM.jpg – BRD

+0

能否請您分享一些代碼.... ??? – Shiladitya

+0

請提供您的[mcve] _在問題中使用文字,而不是images_。 – evolutionxbox

回答

0

下面是一個簡單的例子。 它應該包含所有你需要

$(function() { 
 
    $("#search").on("keyup", function() { 
 
    $(".users").html(""); 
 
    var val = $.trim(this.value); 
 
    if (val) { 
 
     val = val.toLowerCase(); 
 
     $.each(usersArray, function(_,obj) { 
 
     // console.log(val,obj.name.toLowerCase().indexOf(val),obj) 
 
     if (obj.name.toLowerCase().indexOf(val) != -1) { 
 
      $(".users").append('<div class="user-card"><span class="user-info">'+obj.name+'</span><br/><img class="user-image" src="'+obj.image+'"/></div>'); 
 
     } 
 
     }); 
 
    } 
 
    $(".not-found").toggle($(".users").find("img").length==0); 
 
    }); 
 
}); 
 

 

 
var usersArray = [{ 
 
    name: "Jhon Doe", 
 
    age: 21, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Jane Doe", 
 
    age: 20, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Milo Westfall", 
 
    age: 31, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Dayna Bennefield", 
 
    age: 27, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Danny Eckhoff", 
 
    age: 18, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Estella Fosdick", 
 
    age: 51, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Farah Benson", 
 
    age: 77, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Keith Gross", 
 
    age: 21, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Malcolm X", 
 
    age: 20, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Jhon Cena", 
 
    age: 31, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Rich Ross", 
 
    age: 27, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Charlie Sheen", 
 
    age: 44, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Lena Donovan", 
 
    age: 51, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Jay Kos", 
 
    age: 77, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Vincent Robert", 
 
    age: 21, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Adam Rodriguez", 
 
    age: 20, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Jhon Travolta", 
 
    age: 31, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Ben Mackferson", 
 
    age: 27, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Stella Cox", 
 
    age: 18, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Jenna Johnson", 
 
    age: 51, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    }, 
 
    { 
 
    name: "Bill Tim", 
 
    age: 77, 
 
    image: "https://placeimg.com/140/140/people?t=" + Math.random() 
 
    } 
 
];
body { 
 
    margin: 0; 
 
    padding: 0; 
 
    font-family: 'Zilla Slab', serif; 
 
} 
 

 
.container { 
 
    width: 810px; 
 
    margin: 0 auto; 
 
} 
 

 
h2 { 
 
    float: left; 
 
} 
 

 
#search { 
 
    float: right; 
 
    margin-top: 30px; 
 
} 
 

 
.clear { 
 
    clear: both; 
 
} 
 

 
.not-found { 
 
    text-align: center; 
 
    display: none; 
 
} 
 

 
.users { 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
} 
 

 
.user-card { 
 
    flex: 0 0 auto; 
 
    flex-basis: 140px; 
 
    margin: 5px; 
 
    padding: 5px; 
 
    text-align: center; 
 
    border: 1px solid #eee; 
 
    border-radius: 5px; 
 
} 
 

 
.user-image img { 
 
    width: 140px; 
 
    height: 140px; 
 
    border-radius: 5px; 
 
} 
 

 
.user-info { 
 
    margin-top: 10px; 
 
} 
 

 
.user-info .name, .user-info .age { 
 
    margin: 0; 
 
} 
 

 
#template { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<link href="https://fonts.googleapis.com/css?family=Zilla+Slab" rel="stylesheet" /> 
 

 
<div class="container"> 
 
    <h2>Users:</h2> 
 
    <input type="text" id="search" placeholder="Search by name..."> 
 

 
    <div class="clear"></div> 
 

 
    <div id="template" class="user-card"> 
 
    <div class="user-image"> 
 
     <img src=""> 
 
    </div> 
 
    <div class="user-info"> 
 
     <h4 class="name"></h4> 
 
     <h5 class="age"></h5> 
 
    </div> 
 
    </div> 
 

 
    <p class="not-found"> 
 
    No users found, please change search criteria... 
 
    </p> 
 

 
    <section class="users"> 
 

 
    </section> 
 
</div>

0

$(document).ready(function(){ 
 
    $("#filter").keyup(function(){ 
 
    
 
     // Retrieve the input field text and reset the count to zero 
 
     var filter = $(this).val(), count = 0; 
 
     $("#no-count").text(''); 
 
     // Loop through the comment list 
 
     $("nav ul li").each(function(){ 
 
    
 
      // If the list item does not contain the text phrase fade it out 
 
      if ($(this).text().search(new RegExp(filter, "i")) < 0) { 
 
       $(this).fadeOut(); 
 
    
 
      // Show the list item if the phrase matches and increase the count by 1 
 
      } else { 
 
       $(this).show(); 
 
       count++; 
 
      } 
 
     }); 
 
    
 
     // Update the count 
 
     var numberItems = count; 
 
     $("#filter-count").text("Number of Comments = "+count); 
 
     if(count < 1) { 
 
      $("#no-count").text('No result'); 
 
     } else { 
 
      $("#no-count").text(''); 
 
     } 
 
     
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<form id="live-search" action="" class="styled" method="post"> 
 
    <fieldset> 
 
     <input type="text" class="text-input" id="filter" value="" /> 
 
     <span id="filter-count"></span> 
 
     <p id="no-count"></p> 
 
    </fieldset> 
 
</form> 
 

 
<nav> 
 
    <ul> 
 
     <li><a href="#">Jim James</a></li> 
 
     <li><a href="#">Hello Bye</a></li> 
 
     <li><a href="#">Wassup Food</a></li> 
 
     <li><a href="#">Contact Us</a></li> 
 
     <li><a href="#">Bleep bloop</a></li> 
 
     <li><a href="#">jQuery HTML</a></li> 
 
     <li><a href="#">CSS HTML AJAX</a></li> 
 
     <li><a href="#">HTML5 Net Set</a></li> 
 
     <li><a href="#">Node Easy</a></li> 
 
     <li><a href="#">Listing Bloop</a></li> 
 
     <li><a href="#">Contact HTML5</a></li> 
 
     <li><a href="#">CSS3 Ajax</a></li> 
 
     <li><a href="#">ET</a></li> 
 
    </ul> 
 
</nav>

檢查這個例子。我希望它能以各種方式幫助你。

+0

是的,但我沒有使用

+0

我希望你有你的答案。 – soverflow77

相關問題