You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

页面HTML搜索字符串后出现“not found”告警通知求助

Fixing the "Not Found" Alert for Your Search Feature

Got it, the issue here is that your current checkInput() function uses window.find() but doesn’t actually check whether the search returned any results. Let’s fix that by adding logic to detect when the query isn’t found and trigger your alert.

Updated JavaScript Code

function checkInput() {
  // Get and clean up the search query
  var query = document.getElementById('search').value.trim();
  
  // Don't run search if input is empty
  if (!query) {
    alert("Silakan masukkan nomor HP yang ingin dicari!");
    return;
  }
  
  // Perform the search and capture the result
  var searchFound = window.find(query);
  
  // Show alert if nothing was found
  if (!searchFound) {
    alert("Nomor HP tidak ditemukan!");
  }
  
  // Optional: Reset search state so next search starts from the top
  // window.find('', false, false);
}

Key Improvements Explained:

  • Input Trimming: Added .trim() to remove extra spaces, so accidental whitespace doesn’t trigger a false "not found" alert.
  • Empty Input Handling: Prevents unnecessary searches when the user clicks "Cari" without typing anything.
  • Result Check: window.find() returns a boolean (true if the text is found, false otherwise). We use this value to show your alert when the query isn’t present on the page.
  • Optional Search Reset: If you want subsequent searches to start from the beginning of the page instead of where the last search left off, uncomment the final line—it clears the browser’s search state.

Quick Tip for Case Insensitivity:

By default, window.find() is case-sensitive. If you want to ignore capitalization in searches, pass true as the second argument:

var searchFound = window.find(query, true); // Case-insensitive search

That should make your search function properly alert users when their query isn’t found on the page.

内容的提问来源于stack exchange,提问作者yagami cell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:19:28