Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

408
Visualizações
¿Se muestra "No se encontraron resultados" en Javascript Live Search?

Seguí un codepen simple (mi versión: https://codepen.io/naomimekeel/pen/rNzoeGQ ) para crear una búsqueda js en vivo, pero necesito una forma de mostrar un mensaje "Lo siento, no se encontraron resultados" si la entrada no no coincide con ninguno de los elementos de la lista. Sé que este es probablemente un detector de eventos simple, pero no soy competente en javascript y no puedo encontrar ninguna respuesta que coincida con esta forma específica de filtrado. ¿Cuál es la mejor manera de hacer esto?

 <div class="facstaffsearchbar"> <input type="text" id="facstaff-search" name="facstaff-search" rel="facstaff-search" placeholder="🔍 Search for a donor on this list . . ." /> <div rel="facstaff-data" id="facstaff-data"> <div class="facstaff-data-item">+Terry Laughlin KGSB '81 & Regina L. Aldisert<span style="display:none!important;"></span></div> <div class="facstaff-data-item">+Dr. W. Harry Archer, DEN '27, A&S '37 & +Mrs. Louise E. Archer, A&S '27<span style="display:none!important;"></span></div> <div class="facstaff-data-item">+Lauren H. Ashe, A&S '14<span style="display:none!important;"></span></div> <div class="facstaff-data-item">+William R. Baierl, EDUC '51<span style="display:none!important;">Bill, Willy</span></div> <div class="facstaff-data-item">Bettye J. Bailey, CGS '84 & +Ralph E. Bailey<span style="display:none!important;"></span></div> </div>
 const searchInput = document.querySelector('input[rel="facstaff-search"]'); searchInput.addEventListener('input', function (event) { const input = event.target; const query = event.target.value; const results = input.parentNode; const data = results.querySelectorAll(".facstaff-data-item"); if (input.value) { for (let i = 0, len = data.length; i < len; i++) { data[i].innerHTML.toLowerCase().includes(input.value.toLowerCase()) ? data[i].style.display = 'block' : data[i].style.display = 'none'; } } else { for (let i = 0, len = data.length; i < len; i++) { data[i].style.display = 'block'; } } });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Así que agregue un cheque para ver si tiene alguno. Y realmente debería usar ternario para devolver un valor, no como un if/else

 let hasMatches = false; if (input.value) { const textLC = input.value.toLowerCase(); for (let i = 0, len = data.length; i < len; i++) { const dataMatch = data[i].innerHTML.toLowerCase().includes(textLC); data[i].style.display = dataMatch ? 'block' : 'none'; hasMatches = hasMatches || dataMatch; } if (!hasMatches) { console.log('NOPE'); } }
about 4 years ago · Juan Pablo Isaza Relatório

0

Podría hacer algo como esto, crear una variable para verificar si hay coincidencias o no, si hay alguna, actualice la variable

 let foundOne = false; const none = document.getElementById('none'); const searchInput = document.querySelector('input[rel="facstaff-search"]'); const data = document.querySelectorAll(".facstaff-data-item"); show(false); searchInput.addEventListener('input', function (event) { // reset state show(false) foundOne = false; const input = event.target if (input.value) { const toMatch = input.value.toLowerCase(); for (let i = 0, len = data.length; i < len; i++) { const match = data[i].innerHTML.toLowerCase().includes(toMatch); data[i].style.display = match ? 'block' : 'none'; if (match) foundOne = true; } if (!foundOne) show(true); } else resetState() }); function resetState() { for (let i = 0, len = data.length; i < len; i++) data[i].style.display = 'block'; } function show(bool) { none.style.display = bool ? 'block' : 'none'; }
 <div class="facstaffsearchbar"> <input type="text" id="facstaff-search" name="facstaff-search" rel="facstaff-search" placeholder="🔍 Search for a donor on this list . . ." /> <div id="none">sorry no results found</div> <div rel="facstaff-data" id="facstaff-data"> <div class="facstaff-data-item">+Terry Laughlin KGSB '81 & Regina L. Aldisert<span style="display:none!important;"></span></div> <div class="facstaff-data-item">+Dr. W. Harry Archer, DEN '27, A&S '37 & +Mrs. Louise E. Archer, A&S '27<span style="display:none!important;"></span></div> <div class="facstaff-data-item">+Lauren H. Ashe, A&S '14<span style="display:none!important;"></span></div> <div class="facstaff-data-item">+William R. Baierl, EDUC '51<span style="display:none!important;">Bill, Willy</span></div> <div class="facstaff-data-item">Bettye J. Bailey, CGS '84 & +Ralph E. Bailey<span style="display:none!important;"></span></div> </div> </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda