简体   繁体   English

javascript 实时搜索框

[英]javascript real time search box

I have my search box almost finished, I think the logic of the code is correct, but it is throwing me an error on the line that I want to add the "hide" class, if someone would be so kind as to give me a hand I would appreciate it.我的搜索框快完成了,我认为代码的逻辑是正确的,但它在我想添加“隐藏”class 的行上抛出一个错误,如果有人愿意给我一个我会很感激的。

Error:错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'classList') at prueba.js:301:19 at Array.forEach (<anonymous>) at HTMLInputElement.<anonymous> (prueba.js:296:13)

Code:代码:

const traigoJson = async()=> {
  const respuesta = await fetch ('../stock.json')
  const data = await respuesta.json()

  let contenedor = document.getElementById("container")

  data.forEach((producto, indice) => {

  let card = document.createElement("div")

  card.classList.add("col-xl-3", "col-lg-3", "col-md-6", "col-sm-6", "hide", `${producto.deporte}`, "products")
  card.innerHTML = `<div class="glasses_box">
      <figure><img src="${producto.imagen}" alt="esta es una foto de ${producto.nombre}"/></figure>
      <h3><span class="blu">$</span>${producto.precio}</h3>
      <p class="product-name">${producto.nombre}</p>
      <button type="button" class="btn btn-outline-secondary boton-comprar" id="asd" onClick = "agregarAlCarrito (${indice})">COMPRAR</button>
   </div>`
  contenedor.appendChild(card)
  
  producto.card = card;
})
}

traigoJson()


const searchInput = document.getElementById("search-input")
const card = document.querySelectorAll(".products")
searchInput.addEventListener("input", e => {
  const value = e.target.value.toLowerCase()

  productos.forEach(producto => {
    const isVisible =
      producto.nombre.toLowerCase().includes(value) ||
      producto.deporte.toLowerCase().includes(value)

    producto.card.classList.toggle("hide", !isVisible)
  
  })
})

You create a card element for each product, but then do not add the card to the product item.您为每个产品创建了一个卡片元素,但随后没有将卡片添加到产品项目中。 And so the code errors during the search when trying to get the value of producto.card .因此,在尝试获取producto.card的值时,搜索过程中会出现代码错误。

In the code below I've added this line to the forEach loop:在下面的代码中,我将这一行添加到forEach循环中:

producto.card = card;

This change should be enough to get you started.此更改应该足以让您入门。

 let productos = [{ nombre: "1", deporte: "d", precio: "100", imagen: "https://via.placeholder.com/100" }, { nombre: "2", deporte: "d", precio: "100", imagen: "https://via.placeholder.com/100" }, { nombre: "3", deporte: "d", precio: "100", imagen: "https://via.placeholder.com/100" }, { nombre: "4", deporte: "d", precio: "100", imagen: "https://via.placeholder.com/100" }, { nombre: "5", deporte: "d", precio: "100", imagen: "https://via.placeholder.com/100" } ]; const crearCards = () => { let contenedor = document.getElementById("container") productos.forEach((producto, indice) => { let card = document.createElement("div") card.classList.add("col-xl-3", "col-lg-3", "col-md-6", "col-sm-6", "hide", `${producto.deporte}`, "products") card.innerHTML = `<div class="glasses_box"> <figure><img src="${producto.imagen}" alt="esta es una foto de ${producto.nombre}"/></figure> <h3><span class="blu">$</span>${producto.precio}</h3> <p class="product-name">${producto.nombre}</p> <button type="button" class="btn btn-outline-secondary boton-comprar" id="asd" onClick = "agregarAlCarrito (${indice})">COMPRAR</button> </div>` contenedor.appendChild(card) producto.card = card; }) } const searchInput = document.getElementById("search-input") const card = document.querySelectorAll(".products") searchInput.addEventListener("input", e => { const value = e.target.value.toLowerCase() productos.forEach(producto => { const isVisible = producto.nombre.toLowerCase().includes(value) || producto.deporte.toLowerCase().includes(value) producto.card.classList.toggle("hide", ;isVisible) }) }) crearCards();
 .hide { display: none; }
 <div id="container" class="container"> Type 1, 2, 3, 4, or 5 into the search box to test: <input id="search-input" class="form-control my-2" placeholder="search"> <div class="products"></div> </div> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js"></script>

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

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