显示搜索结果
Show search hits
我正在努力学习JavaScript,所以我知道这不是网店的最佳解决方案,但这只是为了学习。我正在做一个搜索功能,你搜索一个类别,如果你有匹配的,结果就会显示出来。我被困在最后一部分了。我该如何编写代码以便显示结果?
这是我的代码:(不介意showItem函数,还没有启动)
$(document).ready(function() {
var gallery = [
{
"img" : "img/gallery/gameboy2.png",
"title" : "GameBoy Color [yellow]",
"about" : "A-ball",
"price" : 99,
"category" : ["Gameboy", "color", "Console", "game"]
},
{
"img" : "img/gallery/phone.png",
"title" : "Hamburger Phone",
"about" : "What is a smartphone?",
"price" : 129,
"category" : ["phone","hamburger"]
},
{
"img" : "img/gallery/gameboy.png",
"title" : "Nintendo GameBoy",
"about" : "Things doesnt get better with time. This is the shit.",
"price" : 499,
"category" : ["Game","Console", "Gameboy"]
},
{
"img" : "img/gallery/game2.png",
"title" : "SEGA",
"about" : "Things doesnt get better with time. This is the shit.",
"price" : 699,
"category" : ["Game","Console", "SEGA"]
},
{
"img" : "img/gallery/gameboy2.png",
"title" : "GameBoy Color [yellow]",
"about" : "A-ball",
"price" : 99,
"category" : ["Gameboy", "color", "Console", "game"]
},
{
"img" : "img/gallery/phone.png",
"title" : "Hamburger Phone",
"about" : "What is a smartphone?",
"price" : 129,
"category" : ["phone","hamburger"]
},
{
"img" : "img/gallery/gameboy.png",
"title" : "Nintendo GameBoy",
"about" : "Things doesnt get better with time. This is the shit.",
"price" : 499,
"category" : ["Game","Console", "Gameboy"]
},
{
"img" : "img/gallery/game2.png",
"title" : "SEGA",
"about" : "Things doesnt get better with time. This is the shit.",
"price" : 699,
"category" : ["Game","Console", "SEGA"]
}
];
var search = document.getElementById("submit_search");
search.addEventListener("click", searchItem);
showItemsList();
/*
Create a li element and append to already existing ul
Show an image of the product, and below the image show product title and price
*/
function showItemsList() {
var ul = document.getElementById("product_list");
for(i =0; i < gallery.length; i++) {
// get the current product
var currentProduct = gallery[i];
// create element li
var li = document.createElement('li');
// create product img
var productImg = document.createElement('img');
productImg.src = currentProduct.img;
productImg.className = "thumbnail";
li.appendChild(productImg);
// create caption
li.appendChild(document.createTextNode(currentProduct.title + " " + currentProduct.price + "kr"));
ul.appendChild(li);
}
}
/*
If someone click on a product, show a larger picture with a caption telling about the product
If you click the picture again, it minimize back to a thumbnail
*/
function showItem() {
}
/*
A searchfield where you can choose between already existing categories.
Show only those items that been search for
*/
function searchItem() {
var ul = document.getElementById("product_list");
var search = document.getElementById("search").value;
for(var x = 0; x < gallery.length; x++){
//Get the current product
var currentProduct = gallery[x];
//get the current product categories
var currentProductCategories = currentProduct.category;
//Loop through each category
for(var z = 0; z < currentProductCategories.length; z++){
//Check if the category is the one we are looking for
if(currentProductCategories[z] == search){
}
}
}
}
});
让您的showItemsList
方法接受您的列表作为参数:
function showItemsList(items) {
var ul = document.getElementById("product_list");
ul.innerHTML = "";
for(i =0; i < items.length; i++) {
// get the current product
var currentProduct = items[i];
// create element li
var li = document.createElement('li');
// create product img
var productImg = document.createElement('img');
productImg.src = currentProduct.img;
productImg.className = "thumbnail";
li.appendChild(productImg);
// create caption
li.appendChild(document.createTextNode(currentProduct.title + " " + currentProduct.price + "kr"));
ul.appendChild(li);
}
}
现在您的searchItem
方法可以使用它:
function searchItem() {
var matches = [];
var ul = document.getElementById("product_list");
var search = document.getElementById("search").value;
for(var x = 0; x < gallery.length; x++){
//Get the current product
var currentProduct = gallery[x];
//get the current product categories
var currentProductCategories = currentProduct.category;
//Loop through each category
for(var z = 0; z < currentProductCategories.length; z++){
//Check if the category is the one we are looking for
if(currentProductCategories[z] == search){
matches.push(currentProduct);
}
}
}
showItemsList(matches);
}
相关文章:
- 检查搜索结果是否存在多次如果是,则在Javascript中只显示一个结果
- 显示可链接的搜索结果+对齐方式
- 为自定义网站创建JavaScript搜索框,创建显示搜索结果的弹出窗口
- 使用 jQuery UI 折叠面板显示 couchDB 数据库搜索结果时出现问题
- 如何将搜索结果显示为“搜索结果”;显示300〃中的1-10〃;
- 谷歌自定义搜索结果通过代理显示错误
- Jquery XML搜索并显示具有特定用户输入条件的结果
- 搜索结果未显示在同一窗口中
- Javascript PHP在点击搜索时出现弹出图像,关闭图像后显示搜索结果
- 是否有用于以最佳间距在行中显示图像搜索结果的 js 库
- 当没有结果返回 JavaScript 搜索时显示消息
- 如何隐藏搜索结果框,我希望它们在点击并激活键控功能时显示
- 从MySQL搜索结果应显示在一个警报框中
- WP作业管理器-显示找到的所有搜索结果的编号
- angularjs不显示动态搜索结果的分页
- 显示搜索结果
- 如何用AngularJS切换搜索结果显示
- 谷歌自定义网站搜索(商业版)如何使搜索结果显示在弹出窗口中
- 在ie9及以下浏览器中,搜索结果显示在较低的页面
- 如何在等待搜索结果显示时添加加载屏幕